-->
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.  [ 3 posts ] 
Author Message
 Post subject: Insertion Cascade Order when parent id is assigned
PostPosted: Thu Nov 20, 2003 1:41 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 1:30 pm
Posts: 27
I have a class Survey with a many-to-one relationship "completionRewards" which I manage manually (no cascading). This one works fine. But I have another one "variables" which I would like to be managed automatically (cascade="all-delete-orphan").

If I create a new Survey(). Then do a couple addVariable()
Then do session.save(survey). The order of insertion is wrong.

It FIRST tries to insert the variables, THEN the survey.
I need it to insert the survey first, then the children.

Thanks


Code:
<class name="com.lsr.eclipse.framework.core.model.Survey" table="survey">
<id column="survey_id" name="id" unsaved-value="any" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property column="name" length="60" name="name" not-null="true" type="java.lang.String"/>
<property column="url_description" length="255" name="urlDescription" type="java.lang.String"/>
<property column="url_redirect" length="255" name="urlRedirect" type="java.lang.String"/>
<property column="comment" length="255" name="comment" type="java.lang.String"/>
<many-to-one column="screener_survey_id" name="screenerSurvey" class="com.lsr.eclipse.framework.core.model.Survey" outer-join="false"/>
<many-to-one column="job_id" name="job" class="com.lsr.eclipse.framework.core.model.Job" outer-join="false"/>
<property column="industry_id" length="6" name="industryId" type="java.lang.Integer"/>
<many-to-one column="tool_type_id" name="toolType" class="com.lsr.eclipse.framework.core.model.ToolType" outer-join="false"/>
<many-to-one column="survey_type_id" name="surveyType" class="com.lsr.eclipse.framework.core.model.SurveyType" outer-join="false"/>
<many-to-one column="survey_status_id" name="surveyStatus" class="com.lsr.eclipse.framework.core.model.SurveyStatus" outer-join="false"/>
<property column="last_mod_dt" length="23" name="lastModDt" not-null="true" type="java.util.Date"/>
<property column="last_mod_userid" length="10" name="lastModUserid" not-null="true" type="java.lang.Integer"/>
<set name="completionRewards" lazy="true" inverse="true">
    <key column="survey_id"/>
    <one-to-many class="com.lsr.eclipse.framework.core.model.SurveyCompletionReward"/>
</set>
<set name="batches" lazy="true" inverse="true" order-by="batch_no asc">
    <key column="survey_id"/>
    <one-to-many class="com.lsr.eclipse.framework.core.model.SurveyBatch"/>
</set>
<bag name="variables" lazy="true" cascade="all-delete-orphan" inverse="true" >
    <key column="survey_id"/>
    <one-to-many class="com.lsr.eclipse.framework.core.model.SurveyVariable"/>
</bag>
</class>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 3:53 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 1:30 pm
Posts: 27
whoops, typo in my question, "completionRewards" and "variables" are one-to-many associations, the mapping xml is correct.

Btw: having unsaved-value="any" in the parent (Survey) and calling session.save(survey) should always save the parent first, no?

Using hibernate 2.1beta6

here is the SurveyVariable mapping, having not-null="true" in the many-to-one causes the following exception...

Code:
java.lang.NullPointerException: not-null property references a null or transient value: com.lsr.eclipse.framework.core.model.SurveyVariable.survey
   at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1144)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:800)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:678)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1324)
   at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:113)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:339)
   at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:429)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:355)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:406)
   at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:385)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:824)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:678)
   at com.lsr.eclipse.framework.core.transaction.impl.TransactionPersistSurvey.execute(TransactionPersistSurvey.java:70)

Code:
<class name="com.lsr.eclipse.framework.core.model.SurveyVariable" table="survey_variable">
<id column="survey_variable_id" name="surveyVariableId" length="10" type="java.lang.Long">
<generator class="native"/>
</id>
<property column="table_name" length="64" name="tableName" not-null="true" type="java.lang.String"/>
<property column="column_name" length="64" name="columnName" not-null="true" type="java.lang.String"/>
<many-to-one column="survey_id" name="survey" class="com.lsr.eclipse.framework.core.model.Survey" not-null="true" outer-join="false"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 9:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you can get this when you combine identity columns with other pk generation strategies. you will need to give Hibernate a little hint by saving and flushing the parent b4 adding the children.


(Identity column inserts can't be done write-behind.)


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