-->
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: Parent / Child save triggering delete
PostPosted: Wed Dec 31, 2008 1:01 am 
Newbie

Joined: Wed Dec 31, 2008 12:48 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.3.1 GA

Mapping documents:
Code:
<hibernate-mapping>
   <class name="ContentItem" table="contentitem">
      <id name="itemID" column="itemID">
         <generator class="native" />
      </id>
<version name="version" column="version" />

....
....
<!-- PARENT CHILD RECURSIVE RELATIONSHIP -->
      <set name="children" inverse="false"  >
         <key column="parent_fk" />
         <one-to-many class="ContentItem" />
      </set>
      
      <!-- PARENT CHILD RECURSIVE RELATIONSHIP -->
      <many-to-one name="parent" class="ContentItem" column="parent_fk" />
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
long now = System.currentTimeMillis();
            ContentItem topic = new ContentItemDAO().findContentItemByID(topicID);
            User author = new UserManager().getByID(authorID);
            topic.setLastActiveDate(now);
            topic.setCommentCount(topic.getCommentCount().intValue() + 1);

            comment.setDateTime(now);

            comment.setAuthor(author);
            comment.setType(ContentItem.DISCUSSION_TYPE);
            comment.setDiscussion(topic.getDiscussion());
            comment.setParent(topic);
            topic.getChildren().add(comment);
            HibUtil.save(comment);
            HibUtil.getCurrentSession().flush();



Name and version of the database you are using:MySQL 5.0.67

The generated SQL (show_sql=true):
Hibernate: select site0_.siteID as siteID10_, site0_.version as version10_, site0_.name as name10_, site0_.host as host10_ from site site0_
Hibernate: select site0_.siteID as siteID10_, site0_.version as version10_, site0_.name as name10_, site0_.host as host10_ from site site0_ where site0_.name=?
Hibernate: select contentite0_.itemID as itemID4_, contentite0_.version as version4_, contentite0_.title as title4_, contentite0_.text as text4_, contentite0_.datetime as datetime4_, contentite0_.changehistory as changehi6_4_, contentite0_.changeid as changeid4_, contentite0_.commentcount as commentc8_4_, contentite0_.lastactivedate as lastacti9_4_, contentite0_.type as type4_, contentite0_.bug_fk as bug11_4_, contentite0_.ticket_fk as ticket12_4_, contentite0_.author_fk as author13_4_, contentite0_.discussion_fk as discussion14_4_, contentite0_.parent_fk as parent15_4_, contentite0_.site_fk as site16_4_ from contentitem contentite0_ where contentite0_.site_fk = ? and contentite0_.itemID=?
Hibernate: select user0_.userID as userID1_, user0_.version as version1_, user0_.name as name1_, user0_.email as email1_, user0_.password as password1_, user0_.role as role1_, user0_.createdate as createdate1_, user0_.org_fk as org8_1_, user0_.site_fk as site9_1_ from user user0_ where user0_.userID=?
Hibernate: insert into contentitem (version, title, text, datetime, changehistory, changeid, commentcount, lastactivedate, type, bug_fk, ticket_fk, author_fk, discussion_fk, parent_fk, site_fk) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update contentitem set version=?, title=?, text=?, datetime=?, changehistory=?, changeid=?, commentcount=?, lastactivedate=?, type=?, bug_fk=?, ticket_fk=?, author_fk=?, discussion_fk=?, parent_fk=?, site_fk=? where itemID=? and version=?


---------------------------------BEGIN PROBLEM----------------------------------------
Hibernate: update contentitem set parent_fk=null where parent_fk=?
------------------------------END PROBLEM-------------------------------------------

Hibernate: update contentitem set version=?, title=?, text=?, datetime=?, changehistory=?, changeid=?, commentcount=?, lastactivedate=?, type=?, bug_fk=?, ticket_fk=?, author_fk=?, discussion_fk=?, parent_fk=?, site_fk=? where itemID=? and version=?




For some reason, when I try to create a child and link it to a parent (recursive relationship) it triggers a delete of the collection by executing the update statement above. Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 2:18 am 
Newbie

Joined: Wed Dec 31, 2008 12:48 am
Posts: 2
I narrowed it down even further to the point where when I load the ContentItem from a query, it executes an update statement.

When the following HQL is executed:
Code:
return (ContentItem) sessionFactory
                .getCurrentSession()
                .createQuery("FROM ContentItem WHERE itemID = :itemID")
                .setInteger("itemID", itemID)
                .uniqueResult();


It causes the following SQL to be generated:

Hibernate: select contentite0_.itemID as itemID14_, contentite0_.version as version14_, contentite0_.title as title14_, contentite0_.text as text14_, contentite0_.datetime as datetime14_, contentite0_.changehistory as changehi6_14_, contentite0_.changeid as changeid14_, contentite0_.commentcount as commentc8_14_, contentite0_.lastactivedate as lastacti9_14_, contentite0_.type as type14_, contentite0_.bug_fk as bug11_14_, contentite0_.ticket_fk as ticket12_14_, contentite0_.author_fk as author13_14_, contentite0_.discussion_fk as discussion14_14_, contentite0_.parent_fk as parent15_14_, contentite0_.site_fk as site16_14_ from contentitem contentite0_ where contentite0_.site_fk = ? and contentite0_.itemID=?
Hibernate: update contentitem set version=?, title=?, text=?, datetime=?, changehistory=?, changeid=?, commentcount=?, lastactivedate=?, type=?, bug_fk=?, ticket_fk=?, author_fk=?, discussion_fk=?, parent_fk=?, site_fk=? where itemID=? and version=?
Hibernate: update contentitem set parent_fk=null where parent_fk=?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.