0

How to persist child entity in oneToMany relationship ?

@Entity
public class Payment implements Serializable {

  @ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
  @JoinColumn(name = "registration", nullable = false)
  private Registration registration;
}

@Entity 
public class Registration implements Serializable {

 @OneToMany(mappedBy="registration", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
 private List<Payment> payment;
}

On registration creation, if registration column is not nullable in Payment table :

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'registration' cannot be null

But if registration is nullable then a payment is created but registration column is null :

enter image description here

Until an exception occurs and "HH000010: On release of batch it still contained JDBC statements" be executed.

Please can you help me, to disabled Hibernate batch or understand what is wrong ?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Mezoo
  • 677
  • 2
  • 10
  • 18

2 Answers2

0

You need to persist Registration entity.

Initialise your Registration object

Registration reg = new Registration();

List<Payment> lstPayment = new ArrayList<Payment>();
for loop...
    Payment pay = new Payment();
    // imp point
    pay.setRegistration(reg);
    lstPayment.add(pay);
for loop ends //

reg.setLstPayment(lstPayment);

//persist Registration entity 
em.persist(reg);
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
  • I'im using spring boot-jpa with CrudRepository. Usually no need to specify child persisting. And In my case, child is Payment, which must persist on Registration creation – Mezoo Nov 23 '16 at 08:20
0

By looking in your code Column 'registration' for table Registration should be primary key and Auto increment. Verify with your table if not so.

Hibernate through this error if primary key of child table is not set as primary key and auto increment in table.

Chandra
  • 1,722
  • 16
  • 19