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
---------------------------------------------------
|