Hibernate 不存在具有给定标识符的行:将多个关系从懒惰更改为渴望时的奇怪行为
当我尝试加载一个实体时,我遇到了一个奇怪的行为,我得到了以下异常Hibernate 不存在具有给定标识符的行:将多个关系从懒惰更改为渴望时的奇怪行为,hibernate,jpa,criteria,Hibernate,Jpa,Criteria,当我尝试加载一个实体时,我遇到了一个奇怪的行为,我得到了以下异常 org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.xxx.entity.Role#4545] 我的hibernate实体看起来非常复杂,我希望我能让它尽可能简单: @Entity class Employee {
org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException:
No row with the given identifier exists: [com.xxx.entity.Role#4545]
我的hibernate实体看起来非常复杂,我希望我能让它尽可能简单:
@Entity
class Employee {
@ManyToMany
private List<BB> bbList = new ArrayList<>();
}
@Entity
class BB extends CC{
}
@Entity
@Inheritance(strategy = JOINED)
abstract class CC {
@ManyToOne(optional = false)
@JoinColumn(name = "ID_XXX_DIM", nullable = false)
private Dimension dimension;
}
@Entity
class Dimension {
@ManyToMany(fetch = FetchType.EAGER)
private List<Role> roles = new ArrayList<Role>();
}
到
此异常的根本原因是Hibernate条件中的错误,该错误为不同的主键生成了具有相同别名的错误SQL查询。 这是生成的查询的快照,您可以看到别名ID2\u 8\u对于不同的表列是重复的
select composite1_.ID_RSK_IND as ID2_8_, dimension8_.MNE_DIM as MNE3_90_4_,
roles9_.ID_GRP_DIM as ID1_90_8_, role10_.ID_ROL as ID2_8_
Oracle可以处理这种情况,但hibernate将尝试用另一个实体的密钥加载该实体
作为解决方法,我发现添加@Fetch(value=FetchMode.SUBSELECT)
将解决这个bug,我不知道为什么,但它是有效的
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Role> roles = new ArrayList<Role>();
@ManyToMany(fetch=FetchType.EAGER)
@Fetch(值=FetchMode.SUBSELECT)
私有列表角色=新的ArrayList();
错误表示外键列上有一个ID,而外键表上没有匹配的PK。DB上有FK约束吗?特别是在多对多联接表中?如果是,请检查它们是否正确。如果它们是正确的,则很可能映射中有错误,因此请添加表的DDL。@这是hibernate条件中的一个错误,它会导致为不同的主键列生成相同的别名,这样我们就可以使用错误的键访问实体。
org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException:
No row with the given identifier exists: [com.xxx.entity.Role#4545]
select composite1_.ID_RSK_IND as ID2_8_, dimension8_.MNE_DIM as MNE3_90_4_,
roles9_.ID_GRP_DIM as ID1_90_8_, role10_.ID_ROL as ID2_8_
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Role> roles = new ArrayList<Role>();