I have encountered a problem when removing a entity from the database. It seems that toplink does not remove the entries in the join table, when deleting entities with @OneToMany multiplicity. The derby database complains about toplink violating the join table constraints. Here is my test code:
EntityA.java:
-----------------
import javax.persistence.*;
@MappedSuperclass
public abstract class EntityA {
private long id;
@Id @GeneratedValue
public long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
}
EntityB.java
-----------------
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
@Entity
public class EntityB extends EntityA {
private List<EntityC> foo = new ArrayList<EntityC>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public List<EntityC> getFoo() {
return foo;
}
public void setFoo(List<EntityC> foo) {
this.foo = foo;
}
public void addFoo(EntityC foo) {
getFoo().add(foo);
}
}
EntityC.java:
------------------
import javax.persistence.Entity;
@Entity
public class EntityC extends EntityA {
String s;
protected EntityC() {
}
public EntityC(String s) {
this.s = s;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
Test.java:
-------------
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
public class Test {
public static void main(String[] args) {
EntityManagerFactory emF = Persistence.createEntityManagerFactory("test");
EntityManager em = emF.createEntityManager();
EntityTransaction tx = em.getTransaction();
EntityB obj;
tx.begin();
obj = new EntityB();
obj.addFoo(new EntityC("First EntityC"));
obj.addFoo(new EntityC("Second EntityC"));
obj.addFoo(new EntityC("Third EntityC"));
em.persist(obj);
em.flush();
em.remove(obj);
tx.commit();
}
}
My persistence.xml:
<persistence xmlns="
http://java.sun.com/xml/ns/persistence"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="test">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>EntityA</class>
<class>EntityB</class>
<class>EntityC</class>
<properties>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="toplink.jdbc.url" value="jdbc:derby:Test;create=true"/>
<property name="toplink.jdbc.user" value="APP"/>
<property name="toplink.jdbc.password" value="APP"/>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
[Message sent by forum member 'hewagn00' (hewagn00)]
http://forums.java.net/jive/thread.jspa?messageID=206645