Well, thanks for the suggestion subu......
if not without a collection then will a
bidirectional one-to-may relationship work out.
I am just fighting with the following mapping:
<class name="xxx.ServerDetails"
lazy="false"
extends="xxx.Entity">
<key column="entityId" />
<property name="Name" column="Name" />
<set name="endPoints" table="SERVER_ENDPOINTS" lazy="false"
inverse="true"
cascade="all">
<cache usage="read-write"/>
<key column="entityId" />
<one-to-many
class="xxx.EndPointCapability"/>
</set>
</class>
<class name="xxx.EndPointCapability"
table="EndPointCapability" lazy="false">
<cache usage="read-write" />
<id name="endPointId" column="endPointId"></id>
<many-to-one name="serverDetails" column="serverDetails"
class="xxx.ServerDetails"
lazy="false" cascade="all" not-null="true"/>
<property name="ename" column="ename" />
</class>
Now I am using Spring HibernateTemplate,
Adding a Server autom automatically adds a EndPoint.
Code:
HibernateTemplate ht = new HibernateTemplate(this.getSessionFactory());
ServerDetails serverDet = new ServerDetails();
serverDet.setEntityId("en1");
EndPointCapability endPoint1 = new EndPointCapability(new Long(101),serverDet);
serverDet.addEndPoint(endPoint1);
ht.save(serverDet);
But, Delete of a Server does not automatically delete the EndPoint.
Code
Code:
HibernateTemplate ht = new HibernateTemplate(this.getSessionFactory());
List serverList = ht.loadAll(ServerDetails.class);
ServerDetails serverDet = (ServerDetails )serverList.get(0);
ht.delete(serverDet);
Quote:
throws Exception DataIntegrityViolationException
Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not delete: [xxx.ServerDetails#en1]; SQL [delete from ServerDetails where entityId=?]; Integrity constraint violation FKA6D55C8D4E0F4DE8 table: ENDPOINTCAPABILITY in statement [delete from ServerDetails where entityId=?]; nested exception is java.sql.SQLException: Integrity constraint violation FKA6D55C8D4E0F4DE8 table: ENDPOINTCAPABILITY in statement [delete from ServerDetails where entityId=?]
java.sql.SQLException: Integrity constraint violation FKA6D55C8D4E0F4DE8 table: ENDPOINTCAPABILITY in statement [delete from ServerDetails where entityId=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2296)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2442)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
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:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:394)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:788)
Now, I am not able to make out what the problem is .Can someone help.
How will the cascade delete work in case of a bidirectional one-to-many relationship.
-Poonam.