Hi, I just tried to test my database and my code with some JUnits.
Cause I'm using a wrapper-class to hide the hibernate stuff from other classes. I normally do the testcases twice.
First just plain, second with the help of the wrapper class.
But that's only important to understand, why I try do the same operations more than once.
To reproduce the behaviour of the failure, I just execute the plain part twice. (Normally first plain and than wrapper code or vice versa, but this should be easier to understand.)
(Database is newly created before)
Code:
for (int i = 0; i < 2; i++) {
/* direct */
this.s = HibernateUtil.openSession();
Criteria c = this.s.createCriteria(CountryModel.class);
org.hibernate.Transaction t = this.s.beginTransaction();
final int rows = c.list().size();
this.s.save(country);
Assert.assertEquals(rows + 1, c.list().size(), 0);
t.commit();
this.s.close();
this.s = HibernateUtil.openSession();
c = this.s.createCriteria(CountryModel.class);
t = this.s.beginTransaction();
this.s.delete(country);
Assert.assertEquals(rows, c.list().size(), 0);
t.commit();
this.s.close();
}
Strange is that the first time it works fine, but the second time I'm getting
an
"org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update."According to the database, it violates the foreign key constraint of this:
Code:
@OneToOne(cascade = javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY)
@Cascade(value = CascadeType.DELETE_ORPHAN)
private Unternehmensmodell unternehmensmodell;
cause the "Unternehmensmodell" with the given id does not exists. I tried with and without " CascadeType.DELETE_ORPHAN", but it didn't made an difference.
Cause it runs ones, the mappings should be OK.
By the way it fails at the save statement.
I would guess that hibernate, isn't aware that the above delete-statement has cascaded and do not use another insert for the ("Unternehmensmodell") the next time.
Is this true? How can I avoid this?
Otherwise I have no idea left, why the second time fails.
Cause db access is done by the user via a gui, I can't exclude that case, as hypothetical.
Thanks a lot in advance.
Greetings Michael