Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA Delete with 2 Interlocking Entity Classes
    primarykey
    data
    text
    <p>I have 2 EJB 3 Entity Beans:</p> <pre><code>@Entity public class Organisation { @Id @Column(length = 64) private String guid; private String name; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @JoinColumn(name = "home_unit_fk", nullable = true) private Unit homeUnit; } @Entity public class Unit { @Id @Column(length = 64) private String guid; private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "organisation_fk", nullable = false) private Organisation organisation; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_unit_fk", nullable = true) private Unit parentUnit; @OneToMany(mappedBy = "parentUnit", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @OrderBy("shortName") @OptimisticLock(excluded = true) private Set&lt;Unit&gt; childUnits; } </code></pre> <p>If I do a delete on the Organisation using standard Dao :</p> <pre><code>public int deleteByGuid(final String guid) { final Query query = entityManager.createQuery("delete from " + getPersistentClass().getName() + " where guid = :guid"); query.setParameter("guid", guid); return query.executeUpdate(); } </code></pre> <p>But I get the following exception:</p> <p>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (<code>config</code>.<code>unit</code>, CONSTRAINT <code>FK27D184F5D4393D</code> FOREIGN KEY (<code>organisation_fk</code>) REFERENCES <code>organisation</code> (<code>guid</code>))</p> <p>I don't get it. What am I doing wrong? Shouldn't JPA/Hibernate perform deletes on both the Unit and the Organisation within the same transaction?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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