-->
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.  [ 1 post ] 
Author Message
 Post subject: Cascading Key Insert
PostPosted: Tue Apr 08, 2008 6:28 pm 
Newbie

Joined: Thu Oct 25, 2007 2:20 am
Posts: 3
Location: Australia
I've searched high and low for a solution to this. From what I understand it should be a pretty simple matter for hibernate to see the key (user_id) on the parent and populate the key on any child objects when doing an insert.

Am I missing something in my mapping to tell hibernate to do this or am I asking too much?

I did try setting cascade="none" on the child and doing the insert in two steps, but that means I'd also need to do the same for updates. For some reason hibernate doesn't support cascade="update" only "save-update".

What's the best approach to take in this scenario?

Hibernate version:

3

Mapping documents:

<class name="User" table="users" lazy="false">
<id name="userId" column="user_id">
<generator class="native"/>
</id>
<timestamp name="dateUpdated" column="date_updated" source="db" access="property" />
<property name="username"/>
<property name="password"/>
<property name="salt"/>
<property name="firstname"/>
<property name="lastname"/>
<property name="email"/>
<property name="phone"/>
<property name="fax"/>
<property name="mobile"/>
<property name="imDetails" column="im_details"/>
<property name="dob" type="date"/>
<property name="gender"/>
<property name="preflang"/>
<property name="otlang"/>
<property name="org"/>
<property name="jobDesc" column="job_desc"/>
<property name="volunteer"/>
<property name="dateCreated" column="date_created" type="date" update="false" insert="false" generated="insert" />
<property name="notes"/>
<property name="signature"/>
<set name="userDepts" inverse="true" cascade="all-delete-orphan"
order-by="dept_id" lazy="false" fetch="subselect">
<key column="user_id" not-null="true" />
<one-to-many class="UserDept"/>
</set>
</class>

<class name="UserDept" table="user_dept" lazy="false" >
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="userId" column="user_id" />
<property name="deptId" column="dept_id" />
<property name="defaultDept" column="default_dept"/>
<property name="dateAdded" column="date_added" type="date" update="false" insert="false" generated="insert" />
<property name="roleId" column="role_id"/>
<property name="coordinatorId" column="coordinator_id"/>
</class>

Code between sessionFactory.openSession() and session.close():

session.save(user);

Full stack trace of any exception that occurs:

07:34:52,515 ERROR [JDBCExceptionReporter] Cannot insert the value NULL into column 'user_id', table 'Manta.dbo.user_dept'; column does not allow nulls. INSERT fails.

Name and version of the database you are using:

MS-SQL 2005

The generated SQL (show_sql=true):

Hibernate: select current_timestamp
Hibernate: insert into users (date_updated, username, password, salt, firstname, lastname, email, phone, fax, mobile, im_details, dob, gender, preflang, otlang, org, job_desc, volunteer, notes, signature) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select user_.date_created as date20_18_ from users user_ where user_.user_id=?
Hibernate: insert into user_dept (dept_id, default_dept, role_id, coordinator_id) values (?, ?, ?, ?)

_________________
---------------------------------------------------
Jason Sheedy
www.bytespring.com
www.jmpj.net
---------------------------------------------------


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

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.