作为解决方法,我发现添加@Fetch(value = FetchMode.SUBSELECT)
可以解决此错误,我不知道为什么,但是可以使用。
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Role> roles = new ArrayList<Role>();
当我尝试加载实体并遇到以下异常时,我面临一个奇怪的行为
org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException:
No row with the given identifier exists: [com.xxx.entity.Role#4545]
我的休眠实体看起来是如此复杂,我希望我可以使其尽可能简单:
@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>();
}
我想做的是使用休眠条件列出Employee
对象。但是它只有在我更改时才有效
@ManyToMany(fetch = FetchType.EAGER)
private List<Role> roles = new ArrayList<Role>();
到
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles = new ArrayList<Role>();
您能告诉我为什么我使用eager属性时会出现异常吗?>
org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException:
No row with the given identifier exists: [com.xxx.entity.Role#4545]
<<此异常的根本原因是Hibernate Criteria中的一个错误,该错误会为不同的主键生成具有相同别名的错误SQL查询。这是生成的查询的快照,您可以看到别名
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_
作为解决方法,我发现添加@Fetch(value = FetchMode.SUBSELECT)
可以解决此错误,我不知道为什么,但是可以使用。
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Role> roles = new ArrayList<Role>();