[英]spring-data-jpa : Found shared references to a collection org.hibernate.HibernateException
I know this question is asked and answered a number of times and also has a workaround我知道这个问题被问过并回答了很多次,也有一个解决方法
like in Found shared references to a collection org.hibernate.HibernateException http://www.java2s.com/Questions_And_Answers/JPA/Collection/HibernateException.htm就像在Found shared references to a collection org.hibernate.HibernateException http://www.java2s.com/Questions_And_Answers/JPA/Collection/HibernateException.htm
but none of the above worked for me.但以上都不适合我。
I am using SpringBoot, JPA, Hibernate
and have mapping as provided below.我正在使用
SpringBoot, JPA, Hibernate
并具有如下提供的映射。
I am retrieving the list of Phone and iterating over it using for loop.我正在检索电话列表并使用 for 循环对其进行迭代。 I am getting the error
Found shared references to a collection: Phone.person
while iterating over phone list我收到错误
Found shared references to a collection: Phone.person
while iterated over phone list
org.hibernate.HibernateException: Found shared references to a collection: Phone.person
at org.hibernate.engine.internal.Collections.processReachableCollection(Collections.java:188) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:50) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:104) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:65) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:59) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:182) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:232) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:92) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
Mapping to Phone
entity映射到
Phone
实体
@Entity
@Table(name = "PHONETABLE")
@Data
public class Phone{
// other column mapping
@ManyToMany
@JoinTable(name = "SomeTable", joinColumns = {
@JoinColumn(name = "column1", referencedColumnName = "personColumn") }, inverseJoinColumns = {
@JoinColumn(name = "id", referencedColumnName = "id", unique = true) })
@JsonIgnore
private Set<Person> person;
}
I am calling the findByIdMethod
to fetch the data from the repository.我正在调用
findByIdMethod
从存储库中获取数据。 I have used lombok
for getters are setters.我用过
lombok
作为吸气剂是二传手。
If anyone knows the solution then it would be great.如果有人知道解决方案,那就太好了。 Thanks;
谢谢;
It appears that you have multiple Phone
instances with the same Set
instance for person
.看来您有多个
Phone
实例,其中person
具有相同的Set
实例。 This can happen when you clone or copy a Phone
, or do something like phone1.setPerson(phone2.getPerson())
.当您克隆或复制
Phone
或执行诸如phone1.setPerson(phone2.getPerson())
类的操作时,可能会发生这种情况。
It's okay for the Person
s to be the same, but not the Set
itself. Person
s 可以相同,但Set
本身不同。 Each phone should create a new instance of a Set
that is only used within the entity.每个电话都应该创建一个仅在实体内使用的
Set
的新实例。
You can make sure of this, by instantiating collections in the constructor or directly after declaration and then never calling the setter for the collection, eg您可以通过在构造函数中或直接在声明之后实例化 collections 来确保这一点,然后从不调用集合的设置器,例如
private Set<Person> person = new HashSet<Person>();
void addPerson(Person person) {
this.person.add(person);
}
and only use addPerson
, never setPerson
(you can add similar helper methods to add multiple persons, or remove a person, etc.).并且只使用
addPerson
,从不使用setPerson
(您可以添加类似的辅助方法来添加多个人,或删除一个人等)。
You can also return a copy of the set instead of the set itself in the getPerson
getter to be sure that the collection instance never leaks out.您还可以在
getPerson
getter 中返回集合的副本而不是集合本身,以确保集合实例永远不会泄漏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.