Home Hibernate OneToMany in MappedSuperclass fails with Null Constraint Violation
Reply: 0

Hibernate OneToMany in MappedSuperclass fails with Null Constraint Violation

user1741
1#
user1741 Published in May 21, 2018, 10:59 am

Based on Why does hibernate save two @OneToMany lists in one table?

Imagine having following simplified code using Hibernate and JPA:

@Entity
class D {
    @Id @GeneratedValue public long id;
}

@MappedSuperclass
abstract class A {
    @Id @GeneratedValue public long id;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "listcontents")
    public List<D> list1;
}

@Entity class B extends A { }
@Entity class C extends A { }

This leads to a join table that looks like this:

b_id | d_id | c_id

where d_id refers to an item from my list and the other ids refer to instances of the according implementing class. Problem: both b_id and c_id are NON_NULL, therefore throwing an exception as soon as hibernate tries to load any class that extends A:

org.postgresql.util.PSQLException: FEHLER: NULL-Wert in Spalte „b_id“ verletzt Not-Null-Constraint
Detail: Fehlgeschlagene Zeile enthält (null, 1114, 779).

Which roughly translates into

Error: NULL-value in column "b_id" violates Non-Null-Constraint. Contents: (null, 1114, 779)

since an object of class B does not have a reference to a C, obviously.

How do I make this work? (each class that extends A should have a persistent unidirectional reference to a List<D> without writing a single character of extra code in the extending class)

I can freely change IDs and annotations to my liking, as long as it's in classes A or D.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317313 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO