I have 3 objects. To call it A, B, C and the fourth, which has a composite key, which is a combination of identifiers.
@Entity public class A { @Id @GeneratedValue(generator = "generator") private String id; } @Entity public class B { @Id @GeneratedValue(generator = "generator") private String id; } @Entity public class C { @Id @GeneratedValue(generator = "generator") private String id; @OneToMany(cascade = CascadeType.ALL, mappedBy = "c", fetch = FetchType.LAZY) private List<ClassWithCompositeKey> relations = new ArrayList<ClassWithCompositeKey>(); } @Entity public class ClassWithCompositeKey { @EmbeddedId protected CompositeKey compositeKey; @JoinColumn(name = "A_ID", insertable = false, updatable = false) @ManyToOne(optional = false) private A a; @JoinColumn(name = "B_ID", insertable = false, updatable = false) @ManyToOne(optional = false) private B b; @JoinColumn(name = "C_ID", insertable = false, updatable = false) @ManyToOne(optional = false) private C c; public ClassWithCompositeKey(A a, B b, C c) { this.a = a; this.b = b; this.c = c; this.compositeKey = new CompositeKey(a.getId(),b.getId(),c.getId()); } } @Embeddable public class CompositeKey { @Basic(optional = false) @Column(name = "A_ID", columnDefinition = "raw") private String aId; @Basic(optional = false) @Column(name = "B_ID", columnDefinition = "raw") private String bId; @Basic(optional = false) @Column(name = "C_ID", columnDefinition = "raw") private String cId; public CompositeKey(String aId, String bId, String cId) { this.aId = aId; this.bId = bId; this.cId = cId; } }
Then when I try to save:
A a = new A(); B b = new B(); C c = new C(); entityManager.persist(a); entityManager.persist(b);
I get an exception "ConstraintViolationException: Column 'C_ID' cannot be null"
This is because c.id is null before the c value is saved, but this value refers to an instance of CompositeKey.
I would like to save the "c" and automatically save the collection of ClassWithCompositeKey. However, now I first need to save the "c", then attach an instance of ClassWithCompositeKey to it and save it. Is it possible (possibly using a different type of mapping) that C and ClassWithCompositeKey are cascaded in the same "persistent" call?
source share