Hello,
I am having a problem with a one-to-many insert.
SQl Server 2000
Hibernate 2.1.1
I have an user object that is mapped via one-many to UserAttribute objects. When I retrieve an already persited user, new up a UserAttribute and add it to the User's UserAttribute collection and try to save, hibernate tries to insert the new UserAttribute, but the foreign key never gets set in the insert statement. I am sure it is a problem with my mapping, but I cannot seem to figure out what I am doing wrong. My mappings are as follows.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.ss.sso.domain.User" table="SSOUSER">
<id name="id" column="ID" type="long">
<generator class="identity"/>
</id>
<property name="userName" column="USERNAME"/>
<property name="password" column="PASSWORD"/>
<property name="firstName" column="FIRSTNAME"/>
<property name="lastName" column="LASTNAME"/>
<property name="email" column="EMAIL"/>
<bag name="attributes" table="USERATTRIBUTE" inverse="true" cascade="all">
<key column="USER_ID"/>
<one-to-many class="com.ss.sso.domain.UserAttribute"/>
</bag>
<bag name="applicationMaps" table="APPLICATIONMAP" inverse="true" cascade="all">
<key column="USER_ID"/>
<one-to-many class="com.ss.sso.domain.ApplicationMap"/>
</bag>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.ss.sso.domain.UserAttribute" table="USERATTRIBUTE">
<id name="id" column="USERATTRIBUTE_ID" type="long">
<generator class="identity"/>
</id>
<property name="name" column="NAME"/>
<property name="value" column="THEVALUE"/>
<property name="description" column="DESCRIPTION"/>
</class>
</hibernate-mapping>
Code:
public void testCreateUser() throws Exception {
User newUser = new User();
newUser.setFirstName("Shon");
newUser.setLastName("Schetnan");
newUser.setUserName("shon.schetnan@scansource.com");
newUser.setPassword("myPassword");
Transaction t = session.beginTransaction();
session.save(newUser);
t.commit();
System.out.println("New User ID = " + newUser.getId());
assertNotNull(newUser.getId());
}
public void testUpdateUser() throws Exception {
Transaction trans = session.beginTransaction();
User user = retrieveUser("shon.schetnan@scansource.com", "myPassword");
user.setEmail("shon.schetnan@charter.net");
UserAttribute att1 = new UserAttribute();
att1.setName("Home Phone");
att1.setValue("645-4715");
user.addAttribute(att1);
trans.commit();
}
private User retrieveUser(String userName, String password) throws Exception {
Criteria criteria = session.createCriteria(User.class);
criteria.add( Expression.eq("userName",userName));
criteria.add( Expression.eq("password", password));
List results = criteria.list();
assertTrue(results.size() > 0);
return (User)results.get(0);
}
Hibernate generated insert statement:
Hibernate: insert into USERATTRIBUTE (NAME, THEVALUE, DESCRIPTION) values (?, ?, ?)
Exception:
Caused by: java.sql.SQLException: [BEA][SQLServer JDBC Driver][SQLServer]Cannot insert the value NULL into column 'USER_ID', table 'SSO.dbo.USERATTRIBUTE'; column does not allow nulls. INSERT fails.
I don't understand why hibernate is not inserting the USER_ID of the user in the USERATTRIBUTE table.
Thanks much!
Shon
|