8

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)
LaurentG
  • 11,128
  • 9
  • 51
  • 66
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • what Hibernate version are you using? – yair Jul 11 '13 at 21:33
  • also please post the full stack trace (at least up to your own code). – yair Jul 11 '13 at 21:34
  • Anyway it looks like Hibernate bug to me. I searched their bug DB but nothing with NPE in `StatefulPersistenceContext.java:853`. You could file a bug then. – yair Jul 11 '13 at 21:51

2 Answers2

8

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

Andrew
  • 81
  • 1
  • 2
  • There's Set in my class too and resolved same Exception by removing the reference each other in HashCode and Equals. – Junjie Feb 24 '16 at 03:18
  • what did you replace it with? – user_mda Jul 05 '17 at 15:50
  • 2
    Thank you so much, I had a `@Data` from lombok and it was generating my `hashCode` method using all the properties. I just added `@EqualsAndHashCode(exclude={"mySet"})` and it worked ! – tehCivilian Oct 24 '17 at 16:27
1

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.

agodinhost
  • 381
  • 4
  • 16
  • This is the right exception. And you can assume that the DAOs are working correctly otherwise and have all necessary getter and setter methods – stackoverflow Jul 08 '13 at 11:30
  • If it only were the lack of an appropriate setter, Hibernate wouldn't throw NPE... – yair Jul 11 '13 at 21:32
  • I don't know why are you saying that, but if it's true, it would still be Hibernate bug. (unless Hibernate documents this behavior - which it probably doesn't). – yair Jul 14 '13 at 22:27