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
but none of the above worked for me.
I am using SpringBoot, JPA, Hibernate
and have mapping as provided below.
I am retrieving the list of Phone and iterating over it using for loop.
I am getting the error Found shared references to a collection: Phone.person
while iterating 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
@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.
I have used lombok
for getters are setters.
If anyone knows the solution then it would be great. Thanks;