How do you retrieve nested Sets?

蓝咒 提交于 2021-02-17 22:08:08

问题


Is there an annotation I'm missing or is this a limitation to hibernate retrieval?

Entities:

class A {
   Long id;
   Set<B> b;

   @ManyToMany(fetch = FetchType.EAGER)
   @JoinTable(name = "A_B", joinColumns = @JoinColumn(name = "A_ID"), inverseJoinColumns = @JoinColumn(name = "B_ID")
   public Set<B> getBs() {
      return b;
   }

}

class B {
   Long id;
   Set<C> c;

   @ManyToMany(fetch = FetchType.EAGER)
   @JoinTable(name = "B_C", joinColumns = @JoinColumn(name = "B_ID"), inverseJoinColumns = @JoinColumn(name = "C_ID")
   public Set<C> getCs() {
      return C;
   }

}

DAOs:

class ADaoImpl {    
   public A load(Long id) {
      return new A((A) session.load(A.class, id);
   }
}

When I attempt to load an A, I get a

Caused by: java.lang.NullPointerException
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:853)

回答1:


I had an issue very similar to this were i had nested sets, and the hashCode methods for each showed up in the stack trace. In the hashCode methods, they referred to each other, so I removed the reference to each object in the hashcode methods, and I no longer got this exceptions




回答2:


I think it is not the complete exception right?

Where is the setter in class A? The b collection in class A is friendly scoped then hibernate will not be able to write/inject it's value. The same goes for the C collection in class B.

Please, copy your hibernate.xml and any other configuration class here. The hibernate/java version are important too.



来源:https://stackoverflow.com/questions/17454891/how-do-you-retrieve-nested-sets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!