-->
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.  [ 2 posts ] 
Author Message
 Post subject: Dummy collection for cascade purposes only
PostPosted: Wed Aug 05, 2009 3:32 am 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
Hi,

I am thinking of technique similar to this http://opensource.atlassian.com/projects/hibernate/browse/HHH-552 but applied to collections. I would like to define a association in hbm but I don't need it at the POJO level. The only purpose of this association (collection) is to define cascade="delete" which will delete all associated objects when its parent is deleted.

There is no update="false" attribute available on collection. I can try to define inverse="true" on the collection together with noop accessor but i am not sure if this is valid solution or rather kind of hack. If one could give me some hint I would be grateful.

This is the view of the situation:

Code:
<class name="Parent">
   <!--
      I would like to make this association "dummy" - non existing at the pojo level, but performing delete cascade when parent is deleted
    -->
   <set name="children" access="org.hibernate.property.NoopAccessor" cascade="delete">   
       <key column="FKparent" />
       <one-to-many class="Child" />
   </set>
</class>

<class name="Child">
     <!-- this end is actualy used by application -->
     <many-to-one name="parent" class="Parent" column="FKparent" not-null="true"  />
</class>


Top
 Profile  
 
 Post subject: Re: Dummy collection for cascade purposes only
PostPosted: Wed Aug 05, 2009 10:37 am 
Newbie

Joined: Wed Aug 05, 2009 9:52 am
Posts: 4
I believe u have dummy collection for cascade delete operation.
Instead of using dummy collection maintain the list of objects getting removed before saving main POJO object. And iterate through removed object in DAO save method and remove associated entity manually.

something like

public void saveOrUpdate(<Object> r) throws CuicHibernateException
{
Transaction transaction = getTransaction();

try
{
getSessionFactory().getCurrentSession().saveOrUpdate(r);

//Delete Fields associated to deleted fields
List<String> removedFields = r.getRemovedFields();

if(removedFields.size() > 0) {
StringBuffer buff = new StringBuffer(32);
for(String str : removedFields)
buff.append(str + " ");

buff.deleteCharAt(buff.length() - 1); //delete last space

/* To delete Field I need to walk association to Filter, and remove it from there,
* otherwise the Field remains in object graph */
String className = Field.class.getName();
Query q = getSessionFactory().getCurrentSession().createQuery("from " + className + " obj where obj.fieldId in (?)");
q.setString(0, buff.toString());

for(Object f1 : q.list() )
{
f1.getParent().removeField(f);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.