-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problems when deleting elements from one-to-many set
PostPosted: Fri Aug 17, 2007 3:58 am 
Newbie

Joined: Fri Jun 01, 2007 9:09 am
Posts: 4
Hi,

I have a two classes in one-to-many relation with the following mappings:

<class name="Prodo" table="PRODO>
...
<set name="features" table="FEATURES_2_PRODO" cascade="delete" lazy="false">
<key column="PRODO_ID" foreign-key="FK_PRODO_FEATURE" />
<one-to-many class="ProdoFeature" />
</set>
</class>


<class name="ProdoFeature" table="FEATURES_2_PRODO">
<composite-id>
<key-many-to-one name="prodo" column="PRODO_ID"
class="Prodo">
<meta attribute="use-in-equals">true</meta>
</key-many-to-one>
...
</composite-id>
</class>

If I want to delete a feature from prodo's set hibernate executes the following sql:

update
FEATURES_2_PRODO
set
PRODO_ID=null
where
PRODO_ID=?

...and the db throws an exception, because PRODO_ID is a part of the primary key and cannot be null. I don't understand, why is it UPDATE, why not DELETE FROM?

I generate my pojos with hbm2java through ant and it works fine.

For removal I already tried:
prodo.getFeatures().clear();
and:
for(Iterator iter =prodo.getFeatures().iterator();iter.hasNext();session.delete(iter.next()));

any tips?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 4:21 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Use cascade="delete-orphan" instead of delete.
"delete" means delete the children when the parent is deleted. "delete-orphan" means delete a child if it is no longer referenced by a parent.
Or delete the children explicitly.


Top
 Profile  
 
 Post subject: NonUniqueObjectException
PostPosted: Fri Aug 17, 2007 10:11 am 
Newbie

Joined: Fri Jun 01, 2007 9:09 am
Posts: 4
Thanks

I've moved forward: and now getting an other exception:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.x.x.ProdoFeature#x.x.x.ProdoFeature@a6a76e4e]

I got that, when i want to set the Set after delete:

for(Iterator iter =prodo.getFeatures().iterator();iter.hasNext();actionSession.delete(iter.next()));
actionSession.save(prodo);
actionSession.refresh(prodo);

Object[] featureData;
for (int i = 0; i < fcs.length; i++) {
featureData = (Object[]) fcs[i];

ProdoFeature feature;
feature = (ProdoFeature) actionSession.createCriteria(
ProdoFeature.class).add(
Expression.eq("code", featureData[0])).add(
Expression.eq("prodo", prodo)).uniqueResult();
if (feature == null) {
feature = new ProdoFeature(featureData[0].toString(), prodo, ((Short) featureData[1]).shortValue());
}
else{
feature.setQuantity(((Short) featureData[1]).shortValue());
}
actionSession.saveOrUpdate(feature);
}


Top
 Profile  
 
 Post subject: A strange NullpointerException
PostPosted: Fri Aug 17, 2007 10:16 am 
Newbie

Joined: Fri Jun 01, 2007 9:09 am
Posts: 4
Now I modified the code:

for(Iterator iter =prodo.getFeatures().iterator();iter.hasNext();actionSession.delete(iter.next()));
prodo.getFeatures().clear();
actionSession.save(prodo);
actionSession.refresh(prodo);

Object[] featureData;
for (int i = 0; i < fcs.length; i++) {
featureData = (Object[]) fcs[i];

ProdoFeature feature;
feature = (ProdoFeature) actionSession.createCriteria(
ProdoFeature.class)
.add(
Expression.eq("code", featureData[0]))
.add(
Expression.eq("prodo", prodo))
.uniqueResult();
if (feature == null) {
feature = new ProdoFeature(featureData[0].toString(), prodo, ((Short) featureData[1]).shortValue());
}
else{
feature.setQuantity(((Short) featureData[1]).shortValue());
}
actionSession.saveOrUpdate(feature);
}

..and it throws a NullPointerExcfeption when I call .uniqueResult(); for createCriteria:

org.hibernate.event.def.DefaultDeleteEventListener.deleteTransientEntity(DefaultDeleteEventListener.java:162)
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:70)
org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:775)
org.hibernate.impl.SessionImpl.delete(SessionImpl.java:758)
org.hibernate.engine.Cascade.deleteOrphans(Cascade.java:355)
org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:324)
org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
org.hibernate.engine.Cascade.cascade(Cascade.java:130)
org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
com.ibm.hu.vac.scisy.businessLogic.action.ProdoModify.run(ProdoModify.java:102)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.