Hi,
I have two entities, Parent and Child. The Parent has a collection of Child entities (a OneToMany relationship). The Child in-turn has a collection of Child entities (a OneToMany relationship with itself). Now, when I delete the parent, I need to delete all the children. Hence I have specified the cascadetype as CascadeType.REMOVE. But on removal, an UPDATE statement(that tries to update the parent PK of the child table) is fired. Since the column representing the parent PK is not-null, I am getting a constraint violation. I have attached the code and the stack trace. Pls let me know where am I going wrong.
Code:
@Entity
@Table(name = "FRWTSTPNT")
public class Parent {
private String name;
private int age;
private String occupation;
private Calendar dateOfBirth;
private Set<Child> children;
private String lastUpdateUser;
private ParentPK parentPK;
/**
* For optimistic locking
*/
private Calendar lastUpdateTime;
public Parent() {
}
public Parent(ParentVO parentVO) throws SystemException, CreateException {
this.age = parentVO.getAge();
this.dateOfBirth = parentVO.getDateOfBirth().toGMTDate();
this.occupation = parentVO.getOccupation();
this.name = parentVO.getName();
this.lastUpdateUser = parentVO.getLastUpdateUser();
this.lastUpdateTime = parentVO.getLastUpdateTime();
this.parentPK = new ParentPK();
PersistenceController.getEntityManager().persist(this);
}
@Column(name = "PNTAGE")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@OneToMany(cascade=CascadeType.REMOVE)
@JoinColumns( { @JoinColumn(name = "PNTIDR", referencedColumnName = "PNTIDR") })
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
@Column(name = "PNTDOB")
@Temporal(TemporalType.DATE)
public Calendar getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Calendar dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Column(name = "PNTNAM")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "PNTOCP")
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
@Column(name = "LSTUPDTIM")
@Temporal(TemporalType.TIMESTAMP)
@Version
public Calendar getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Calendar lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
@Column(name = "LSTUPDUSR")
public String getLastUpdateUser() {
return lastUpdateUser;
}
public void setLastUpdateUser(String lastUpdateUser) {
this.lastUpdateUser = lastUpdateUser;
}
@EmbeddedId
@AttributeOverride(name = "id", column = @Column(name = "PNTIDR"))
public ParentPK getParentPK() {
return parentPK;
}
public void setParentPK(ParentPK parentPK) {
this.parentPK = parentPK;
}
public static Parent find(long id) throws FinderException, SystemException {
ParentPK parentPK = new ParentPK();
parentPK.setId(id);
Parent parent = PersistenceController.getEntityManager().find(
Parent.class, parentPK);
return parent;
}
public void remove() throws RemoveException, SystemException {
// Call to a utility class that manages the session
PersistenceController.getEntityManager().remove(this);
}
}
@Embeddable
public class ParentPK implements Serializable {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean equals(Object other) {
return this.hashCode() == other.hashCode();
}
public int hashCode() {
return new StringBuilder().append(id).toString().hashCode();
}
}
@Entity
@Table(name = "FRWTSTCLD")
public class Child {
private ChildPK childPK;
private String name;
private int age;
private Calendar dateOfBirth;
private String lastUpdateUser;
private Set<Child> children;
/**
* For optimistic locking
*/
private Calendar lastUpdateTime;
private long parentId;
private long childId;
public Child() {
}
public Child(ChildVO childVO, ParentPK parentPK) throws CreateException, SystemException {
this.childPK = new ChildPK();
childPK.setParentId(parentPK.getId());
this.name = childVO.getName();
this.age = childVO.getAge();
this.dateOfBirth = childVO.getDateOfBirth();
this.lastUpdateUser = childVO.getLastUpdateUser();
PersistenceController.getEntityManager().persist(this);
}
public Child(ChildVO childVO, ChildPK pk) throws CreateException, SystemException {
this.childPK = new ChildPK();
childPK.setParentId(pk.getId());
this.childId = pk.getId();
this.parentId = pk.getParentId();
this.name = childVO.getName();
this.age = childVO.getAge();
this.dateOfBirth = childVO.getDateOfBirth();
this.lastUpdateUser = childVO.getLastUpdateUser();
PersistenceController.getEntityManager().persist(this);
}
@Column(name="CHDAGE")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Column(name="CHDDOB")
@Temporal(TemporalType.DATE)
public Calendar getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Calendar dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Column(name = "CHDNAM")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "LSTUPDTIM")
@Temporal(TemporalType.TIMESTAMP)
@Version
public Calendar getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Calendar lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
@Column(name = "LSTUPDUSR")
public String getLastUpdateUser() {
return lastUpdateUser;
}
public void setLastUpdateUser(String lastUpdateUser) {
this.lastUpdateUser = lastUpdateUser;
}
@EmbeddedId
@AttributeOverrides(
{
@AttributeOverride(name = "parentId", column = @Column(name = "PNTIDR")),
@AttributeOverride(name = "id", column = @Column(name = "CHDIDR"))
}
)
public ChildPK getChildPK() {
return childPK;
}
public void setChildPK(ChildPK childPK) {
this.childPK = childPK;
}
public void remove() throws RemoveException, SystemException {
PersistenceController.getEntityManager().remove(this);
}
@OneToMany
@JoinColumns( {
@JoinColumn(name = "PNTCHDIDR", referencedColumnName = "CHDIDR"),
@JoinColumn(name = "PNTPNTIDR", referencedColumnName = "PNTIDR")
})
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
@Column(name = "PNTPNTIDR", nullable = false)
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
@Column(name = "PNTCHDIDR", nullable = false)
public long getChildId() {
return childId;
}
public void setChildId(long childId) {
this.childId = childId;
}
}
@Embeddable
public class ChildPK implements Serializable {
private long parentId;
private long id;
@Key(generator = "ID_GEN", startAt = "1")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@KeyCondition(column = "PNTIDR")
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
public boolean equals(Object other) {
return (this.hashCode() == other.hashCode());
}
public int hashCode() {
return new StringBuilder().append(parentId).append(id).toString().hashCode();
}
Trace:
Hibernate: update FRWTSTCLD set PNTIDR=null where PNTIDR=?
1194325692904|0|0|statement|update FRWTSTCLD set PNTIDR=null where PNTIDR=?|update FRWTSTCLD set PNTIDR=null where PNTIDR=1
org.hibernate.exception.GenericJDBCException: could not delete collection: [com.ibsplc.icargo.business.test.defaults.Parent.children#component[id]{id=
1}]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1071)
at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:28)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at com.ibsplc.xibase.server.framework.persistence.hibernate.HibernateJTATxWrapper.commit(HibernateJTATxWrapper.java:107)
at com.ibsplc.xibase.server.framework.persistence.tx.TransactionManager.workOnAfter(TransactionManager.java:145)
at com.ibsplc.xibase.server.framework.tx.TransactionProcessor.callManagersAfter(TransactionProcessor.java:228)
at com.ibsplc.xibase.server.framework.tx.TransactionProcessor.postProcess(TransactionProcessor.java:101)
at com.ibsplc.xibase.server.framework.tx.TransactionProcessor.doProcess(TransactionProcessor.java:161)
at com.ibsplc.xibase.server.framework.frontcontroller.FrontControllerEJB.processRequest(FrontControllerEJB.java:139)
at com.ibsplc.xibase.server.framework.frontcontroller.FrontControllerEJB.doProcess(FrontControllerEJB.java:99)
at com.ibsplc.xibase.server.framework.frontcontroller.FrontControllerEJB_w7w9ww_EOImpl.doProcess(FrontControllerEJB_w7w9ww_EOImpl.java:60)
at com.ibsplc.xibase.server.framework.frontcontroller.FrontControllerEJB_w7w9ww_EOImpl_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:517)
at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:224)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:407)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:403)
at weblogic.rmi.internal.BasicServerRef.access$300(BasicServerRef.java:56)
at weblogic.rmi.internal.BasicServerRef$BasicExecuteRequest.run(BasicServerRef.java:934)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)
Caused by: java.sql.SQLException: ORA-01407: cannot update ("FRMWRKTST"."FRWTSTCLD"."PNTIDR") to NULL
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:626)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:185)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_rows(T4CPreparedStatement.java:633)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1090)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2905)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:2977)
at com.p6spy.engine.logging.P6LogPreparedStatement.executeUpdate(P6LogPreparedStatement.java:183)
at weblogic.jdbc.wrapper.PreparedStatement.executeUpdate(PreparedStatement.java:128)
at com.ibsplc.xibase.server.framework.persistence.sql.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:201)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1048)
... 26 more
}