我正在尝试使用Spring数据中的JPQL优化一个基本查询,以避免对数据库进行多个查询,并使用JOIN Fetch在一个查询中检索所有信息,我一直得到以下异常:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id ,className=com.medifast.entity.core.Country}}] [select count(ca) from com.medifast.entity.core.CoApplicant ca LEFT JOIN FETCH ca.country LEFT JOIN FETCH ca.state where ca.client.id = :clientId]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id ,className=com.medifast.entity.core.Country}}] [select count(ca) from com.medifast.entity.core.CoApplicant ca LEFT JOIN FETCH ca.country LEFT JOIN FETCH ca.state where ca.client.id = :clientId]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:301)
这是我的道:
/**
* Interface to handle persistence operations for CoApplicants.
*
*/
@Repository
public interface ICoApplicantDao extends JpaRepository<CoApplicant, Long>
{
@Query("select ca from CoApplicant ca JOIN FETCH ca.country c where ca.client.id = :clientId")
Page<CoApplicant> findCoApplicantsByClient(@Param("clientId") Long clientId, Pageable pageable);
}
这些是我的实体:
@Entity
@Table(name = "BUSINESS.COAPPLICANTS")
@SQLDelete(sql = "UPDATE BUSINESS.COAPPLICANTS SET DELETED = 1 WHERE id = ?")
@Where(clause = "DELETED <> 1")
@Data
@EqualsAndHashCode(callSuper = true)
public class CoApplicant extends AbstractAuditableEntity
{
/**
* Serial.
*/
private static final long serialVersionUID = -297231024073091062L;
/**
* First name.
*/
@Column(name = "FirstName")
private String firstName;
/**
* Last name.
*/
@Column(name = "LastName")
private String lastName;
/**
* Recognition Name.
*/
private String recognitionName;
/**
* For the address line 1 field.
*/
private String addressLine1;
/**
* For the address line 2 field.
*/
private String addressLine2;
/**
* City.
*/
private String city;
/**
* State.
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "State_Id")
private State state;
/**
* Zip Code.
*/
private String zipCode;
/**
* Country.
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Country_Id")
private Country country;
/**
* Email address.
*/
@Column(unique = true)
private String email;
/**
* Main Phone number.
*/
private String mainPhone;
/**
* Constructor.
*/
public CoApplicant()
{
super();
}
}
/**
* Country entity.
*/
@Entity
@Table(name = "Business.Countries")
@SQLDelete(sql = "Update Business.Countries set deleted = 1 where id=?")
@Where(clause = "deleted <> 1")
@Data
@EqualsAndHashCode(callSuper = true)
public class Country extends AbstractAuditableEntity
{
/**
* Serial.
*/
private static final long serialVersionUID = -267110442898674427L;
/**
* Name of the country.
*/
private String name;
/**
* The orders for the client.
*/
@OneToMany(mappedBy = "country", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Collection<State> states;
/**
* Const.
*/
public Country()
{
super();
}
}
/**
* State entity.
*/
@Entity
@Table(name = "Business.States")
@SQLDelete(sql = "Update Business.States set deleted = 1 where id=?")
@Where(clause = "deleted <> 1")
@Data
@EqualsAndHashCode(callSuper = true)
public class State extends AbstractAuditableEntity
{
/**
* Serial.
*/
private static final long serialVersionUID = 8643487990581006632L;
/**
* Name of the state.
*/
private String name;
/**
* Code of the state.
*/
private String code;
/**
* Country to which this State belongs.
*/
@ManyToOne
@JoinColumn(name = "Country_Id")
private Country country;
/**
* Constr.
*/
public State()
{
super();
}
}
如有任何见解和帮助,将不胜感激。
我想要实现的纯SQL如下所示:
SELECT ca.Id
,firstName
,lastName
,recognitionName
,addressLine1
,city
,email
,mainPhone
,coun.name
,sta.name
FROM coApplicants AS ca
LEFT JOIN countries AS coun
on ca.country_Id=coun.id
LEFT JOIN states AS sta
on ca.state_Id=sta.id
使用带有JOIN FETCH
的可分页查询是Hibernate的一个问题。
要解决这种情况,您必须放置countQuery,几乎与原始的一样,但不相关的内部联接(最终获取)被删除。
可以在此处找到解决方案的来源:带分页的Spring数据获取连接不起作用
因此,您的解决方案是:
@Query(
value="select ca from CoApplicant ca JOIN FETCH ca.country c where ca.client.id = :clientId",
countQuery="select count(ca) from CoApplicant ca where ca.client.id = :clientId")
Page<CoApplicant> findCoApplicantsByClient(@Param("clientId") Long clientId, Pageable pageable);
问题内容: 在JPA注释参考的示例部分中: 示例1-59 @OneToMany-具有泛型的客户类 示例1-60 @ManyToOne-具有泛型的Order类 在我看来,实体是协会的所有者。但是,在同一文档中对属性的说明中写道: 如果关系是双向的,则将关联的反(非所有权)侧的maptedBy元素设置为拥有该关系的字段或属性的名称,如示例1-60所示。 但是,如果我没有记错,则在示例中看起来,实际上是
我有一个这样的家长班 像这样的孩子 现在我想讲述另一个班级的孩子,像这样 它总是给我以下错误: [教义\通用\注释\注释异常] [创建错误]在属性zmpim\Entity\Fiel$value上声明的注释@ORM\ManyToOned::没有名为"mappdBy"的属性。可用属性:塔吉特实体,级联,获取,反转 两者都得到了mappdby,...所以这个错误似乎是毫无意义的 更新: 字段有值和标签。
有一个实体框: 和实体内容: 如果我把 (级联=级联类型。删除) 在Box的“@ManyToOne”中,我删除了该Box,保留了子实体(使Box内容可用于另一个Box)。 相反,如果我使用 (级联=级联型。全部) 将删除框,包括子实体(删除框及其内容)。 假设在某些情况下,需要删除框及其内容。尽管相反,有时我只需要删除框,但保留其内容。 如何根据以上内容选择所需的级联类型?
我在将带有@ManyToOne关系的实体(雇员)映射到带有@OneToMany关系的实体(社区)时遇到了问题。 当我将一个实体(社区)分配给一个实体(员工),其中社区的值为空时,它工作正常。 问题出现了,如果雇员已经为社区分配了价值。当我更改该值并保存更改时,该员工为社区获得了新的值,并且这个新社区在集合中获得了该员工。唯一的问题是,老社区仍然有这个员工在收集,但它应该被删除。这只有在数据库重新启
我怎样才能解决这个问题? 当我尝试时:
我有一个关于Hibernate ManyToOne协会的问题。 我有一个产品实体映射如下: 所以你怎么能看到我们与ProductClass表的关系。和FK我们存储在产品表中。当我创建查询,选择产品的特定类别和价格我有下一个this_: 我接下来的问题是:如何避免Product和ProductCategory这两个表的内部联接? 我的标准DAO代码:<代码>标准。createAlias(“类别”、“