简体   繁体   English

spring-data-jpa:找到对集合 org.hibernate.HibernateException 的共享引用

[英]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.

相关问题 引起:org.hibernate.HibernateException:找到对集合的共享引用 - Caused by: org.hibernate.HibernateException: Found shared references to a collection 找到对集合 org.hibernate.HibernateException 的共享引用 - Found shared references to a collection org.hibernate.HibernateException 多对多集-添加条目-org.hibernate.HibernateException:找到对集合的共享引用: - many to many set - add entry - org.hibernate.HibernateException: Found shared references to a collection: org.hibernate.HibernateException:找不到会话 - org.hibernate.HibernateException: No Session found Hibernate在entitymanager.flush()上抛出“ org.hibernate.HibernateException:找到相同集合的两个表示形式” - Hibernate throws “org.hibernate.HibernateException: Found two representations of same collection” on entitymanager.flush() Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread 春季-&gt; org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联 - spring -> org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions org.hibernate.HibernateException:/hibernate.cfg.xml找不到 - org.hibernate.HibernateException: /hibernate.cfg.xml not found Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread Spring Security authenticationFailure:错误org.hibernate.HibernateException:没有找到当前线程的会话 - Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM