NamedEntityGraph:Hibernate 抛出 cannot simultaneous fetch multiple bags exception for Nested collection
NamedEntityGraph: Hibernate throwing cannot simultaneously fetch multiple bags exception for Nested collection
我得到 "cannot simultaneously fetch multiple bags" 嵌套集合。
这是实体和实体图定义的样子。
休眠版本:4.3.7
spring-数据-jpa:1.8.1
'
@Entity
@Table(name = "ORGANISATION_GROUP")
@Where(clause = "IS_DELETED <> 1")
@NamedEntityGraphs(value = { @NamedEntityGraph(name = "onlyOrganisations", attributeNodes = { @NamedAttributeNode(value = "organisations", subgraph = "lightOrganisation") },
subgraphs = { @NamedSubgraph(name = "lightOrganisation", attributeNodes = { @NamedAttributeNode(value = "locations") }) }) })
public class OrganisationGroup implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private Long id;
@ManyToMany()
@AuditJoinTable(name = "ORGANISATION_GROUP_MEMBER_AUD")
@JoinTable(name = "ORGANISATION_GROUP_MEMBER", joinColumns = @JoinColumn(name = "ORGANISATION_GROUP_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(
name = "ORGANISATION_ID", referencedColumnName = "ID"))
private List<Organisation> organisations = new ArrayList<>(0);
}
@Entity
@Table(name = "ORGANISATION")
public class Organisation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "name", length = 255, nullable = false)
private String name;
@OneToMany( mappedBy = "organisationId", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy(value = "location ASC")
private List<OrganisationLocation> locations;
}
@Entity
@Table(name = "ORGANISATION_LOCATIONS")
public class OrganisationLocation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "LOCATION")
private String location;
}
`
我尝试在 locations 属性上使用 @LazyCollection 但仍然遇到相同的异常。然而,同样的事情在没有实体图和将位置设置为获取急切类型的情况下也有效。如何使用实体图解决此错误?
除非您有充分的理由不这样做,否则请尝试将您的 collections 更改为使用 Set
而不是 List
。
我得到 "cannot simultaneously fetch multiple bags" 嵌套集合。 这是实体和实体图定义的样子。 休眠版本:4.3.7 spring-数据-jpa:1.8.1 '
@Entity
@Table(name = "ORGANISATION_GROUP")
@Where(clause = "IS_DELETED <> 1")
@NamedEntityGraphs(value = { @NamedEntityGraph(name = "onlyOrganisations", attributeNodes = { @NamedAttributeNode(value = "organisations", subgraph = "lightOrganisation") },
subgraphs = { @NamedSubgraph(name = "lightOrganisation", attributeNodes = { @NamedAttributeNode(value = "locations") }) }) })
public class OrganisationGroup implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private Long id;
@ManyToMany()
@AuditJoinTable(name = "ORGANISATION_GROUP_MEMBER_AUD")
@JoinTable(name = "ORGANISATION_GROUP_MEMBER", joinColumns = @JoinColumn(name = "ORGANISATION_GROUP_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(
name = "ORGANISATION_ID", referencedColumnName = "ID"))
private List<Organisation> organisations = new ArrayList<>(0);
}
@Entity
@Table(name = "ORGANISATION")
public class Organisation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "name", length = 255, nullable = false)
private String name;
@OneToMany( mappedBy = "organisationId", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy(value = "location ASC")
private List<OrganisationLocation> locations;
}
@Entity
@Table(name = "ORGANISATION_LOCATIONS")
public class OrganisationLocation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "LOCATION")
private String location;
}
` 我尝试在 locations 属性上使用 @LazyCollection 但仍然遇到相同的异常。然而,同样的事情在没有实体图和将位置设置为获取急切类型的情况下也有效。如何使用实体图解决此错误?
除非您有充分的理由不这样做,否则请尝试将您的 collections 更改为使用 Set
而不是 List
。