Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA, compound key with foreign keys + persist oneToMany
    primarykey
    data
    text
    <p>I'm trying to do something with JPA that'll use a lot on a project but I'm stuck.</p> <p>I have 2 entities + a kind of "glue" entity, I'll call them</p> <ul> <li>ClassA</li> <li>ClassB</li> <li>Glue</li> </ul> <p>I want to add a new ClassA with new Glues set in it's list, ClassB's already exist.</p> <p>That would do something like :</p> <pre> ClassA 1 | Glue 1 1 | ClassB 1 ClassA 1 | Glue 1 2 | ClassB 2 ClassA 1 | Glue 1 3 | ClassB 3 ClassA 1 | Glue 1 4 | ClassB 4 </pre> <p>So as said ClassA and all Glues are to be inserted, ClassA has a List with the new Glues to be inserted.</p> <p>Here they are :</p> <pre><code>@Entity public class ClassA implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; (...) @OneToMany(cascade = CascadeType.ALL, mappedBy = "classA") private List&lt;Glue&gt; glueList; (...) } @Entity public class ClassB implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; (...) } @Entity public class Glue implements Serializable { @EmbeddedId protected GluePK gluePK; @JoinColumn(name = "id_class_a", referencedColumnName = "id", nullable = false, insertable = false, updatable = false) @ManyToOne(optional = false) private ClassA classA; @JoinColumn(name = "id_class_b", referencedColumnName = "id", nullable = false, insertable = false, updatable = false) @ManyToOne(optional = false) private ClassB classB; (...) } @Embeddable public class GluePK implements Serializable { @Basic(optional = false) @NotNull @Column(name = "id_class_a", nullable = false) private int idClassA; @Basic(optional = false) @NotNull @Column(name = "id_class_b", nullable = false) private int idClassB; (...) } </code></pre> <p>When I try to persist my ClassA I'm getting something like :</p> <p>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (<code>bdd</code>.<code>glue</code>, CONSTRAINT <code>constraint_name</code> FOREIGN KEY (<code>id_class_a</code>) REFERENCES <code>ClassA</code> (<code>id</code>) ON DELETE NO ACTION ON UPDATE NO ACTION)</p> <p>I understand that he complains that Glues dont have ClassA's reference set but I'd like him to fill it just then he persists ClassA.</p> <p>Is this achievable? If not what's the best way to do it?</p> <p>I'd like to stay on JPA without any specific vendor tricks (I'm using eclipselink) but if some vendor can do it easily I'll go for it.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload