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?