-->
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: one-to-many insert problem with SQL Server
PostPosted: Thu Jan 29, 2004 1:57 pm 
Newbie

Joined: Thu Sep 18, 2003 2:26 pm
Posts: 5
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


Top
 Profile  
 
 Post subject: many-to-one missing
PostPosted: Thu Jan 29, 2004 2:49 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hi

I guess your UserAttribute mapping is missing the many-to-one mapping information. You'll find more information about this in the manual chapter 9.2 .

HTH
Ernst


Top
 Profile  
 
 Post subject: Many to one
PostPosted: Thu Jan 29, 2004 3:29 pm 
Newbie

Joined: Thu Sep 18, 2003 2:26 pm
Posts: 5
Ernst,

Thanks for your reply!

You were right. It turns out it will work fine "if" I setup a reverse reference in my UserAttribute class to the User, then make sure to set the user reference on the UserAttribute object. Then everything works like I would expect.

Another way that it seems to work, although differently, is to remove the "NOT NULL" constraint from the USER_ID foreign key on UserAttribute. Then, it looks like hibernate inserts the UserAttribute records without the foreign key and goes back and does an update on them later. It seems kind of strange to me, but it works and I guess I can see the logic of why it is doing that. I guess if I want hibernate to handle everything for me I am going to have to remove my NOT NULL constraint. Otherwise, I'll need to add a reverse reference to the parent and make sure to set that referece before adding to the collection.

Shon


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.