Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you retrieve nested Sets?

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)
like image 751
stackoverflow Avatar asked Jul 03 '13 17:07

stackoverflow


2 Answers

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

like image 196
Andrew Avatar answered Nov 20 '22 09:11

Andrew


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.

like image 1
agodinhost Avatar answered Nov 20 '22 09:11

agodinhost