Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Constraint violation orphan removal / cascade(Cascaded deletion triggered by orphan removal causes constraint violation)
Constraint violation orphan removal / cascade [message #846133] Sun, 15 April 2012 20:08
Christoph Smaul is currently offline Christoph SmaulFriend
Messages: 7
Registered: April 2012
Junior Member
I have the following entity classes (I tried to simplify the scenario as much as I could, but after any further simplification, the error does not occur):

@Entity
public class A {

    private static final long serialVersionUID = 4683115568905133791L;

    @Id
    @GeneratedValue
    private long id;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval=true)
    private AbstractB b;

    public AbstractB getB() {
        return b;
    }

    public void setB(AbstractB b) {
        this.b = b;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractB {
    private static final long serialVersionUID = -3333054090847096403L;

    @Id
    @GeneratedValue
    protected long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


}

@Entity
public class BImpl extends AbstractB {
    private static final long serialVersionUID = 4561814816357448235L;
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval=true)
    private D d;

    public D getD() {
        return d;
    }

    public void setD(D d) {
        this.d = d;
    }
}

@Entity
public class D {
    private static final long serialVersionUID = 4561814816357448235L;

    @Id
    @GeneratedValue
    private long id;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @Size(min = 1)
    private Set<C> cs = new HashSet<C>();

    public Set<C> getCs() {
        return cs;
    }

    public void setCs(Set<C> cs) {
        this.cs = cs;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

@Entity
public class C {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}


with an instance of A referencing a BImpl, which references a D, which in turn holds an instance of C in a set.

As you can see in the code, all references have orphanremoval set to true and CascadeType.ALL.
When I set the reference of A->AbstractB to null and merge back the instance of A, the following constraint violation occurs:

    20:05:03,388 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.388--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--UPDATE A SET B_ID = ? WHERE (ID = ?)
    20:05:03,388 INFO  [STDOUT] 	bind => [null, 1]
    20:05:03,390 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.39--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM BIMPL WHERE (ID = ?)
    20:05:03,390 INFO  [STDOUT] 	bind => [2]
    20:05:03,391 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.391--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM ABSTRACTB WHERE (ID = ?)
    20:05:03,391 INFO  [STDOUT] 	bind => [2]
    20:05:03,392 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.392--ClientSession(2132156535)--Connection(1281180651)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--DELETE FROM C WHERE (ID = ?)
    20:05:03,393 INFO  [STDOUT] 	bind => [4]
    20:05:03,393 INFO  [STDOUT] [EL Fine]: 2012-04-13 20:05:03.393--ClientSession(2132156535)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--SELECT 1
    20:05:03,394 INFO  [STDOUT] [EL Warning]: 2012-04-13 20:05:03.394--UnitOfWork(2142376869)--Thread(Thread[http-0.0.0.0-443-2,5,jboss])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
    20:05:03,394 INFO  [STDOUT] Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`db`.`D_C`, CONSTRAINT `FK_D_C_cs_ID` FOREIGN KEY (`cs_ID`) REFERENCES `C` (`ID`))
    20:05:03,394 INFO  [STDOUT] Error Code: 1451
    20:05:03,395 INFO  [STDOUT] Call: DELETE FROM C WHERE (ID = ?)
    20:05:03,395 INFO  [STDOUT] 	bind => [4]


Could anyone explain to me, why the D_C entry does not get deleted before the C instance?
Previous Topic:Running JPA 1 & 2 in same Process
Next Topic:Persisting new entity or changing an entity
Goto Forum:
  


Current Time: Fri Apr 26 04:05:15 GMT 2024

Powered by FUDForum. Page generated in 0.02821 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top