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: PRoblem with foreign key and mapping
PostPosted: Wed Dec 27, 2006 6:03 am 
Newbie

Joined: Wed Dec 27, 2006 5:58 am
Posts: 2
Hello everybody,
I'm a beginner in Hibernate and I'm trying to implement a simple
application. At present time I created a MySQL DB and through Eclipse
plugins I generated mapping files. (the DB is pre existing). The
mapping files that I generated contains the following lines (some table column has been removed, in order to ake shorter the post):

<hibernate-mapping>
<class name="com.biz.eserciziojsp.Useraccesstable"
table="useraccesstable" catalog="eserciziojsp">
<id name="idUser" type="int">
<column name="idUser" /><generator class="assigned" />
</id>
<property name="userName" type="string">
<column name="userName" length="45" not-null="true"/>
</property>

<set name="userinfos" inverse="true">
<key>
<column name="fkIdUserAccess" not-null="true"/>
</key>
<one-to-many class="com.biz.eserciziojsp.Userinfo" />
</set>
</class>
</hibernate-mapping>

and

<hibernate-mapping>
<class name="com.biz.eserciziojsp.Userinfo" table="userinfo"
catalog="eserciziojsp">
<comment></comment>
<id name="idUser" type="int">
<column name="idUser" />
<generator class="assigned" />
</id>
<many-to-one name="useraccesstable"
class="com.biz.eserciziojsp.Useraccesstable" fetch="select">
<column name="fkIdUserAccess" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>

Now I would like to create a new Userinfo and Useraccesstable element
and make them persistent. The code that I implemented is the following
one.

try
{
localSession = HibernateUtil.getSessionFactory().openSession();
Transaction tx = localSession.beginTransaction();

Useraccesstable localUser = new Useraccesstable();
Userinfo localUserinfo = new Userinfo();

localUser.setUserName(username);

// dopo aver inizializzato l'oggetto localUser, inizializzo il
Set per rendere
// l'associazione bidirezionale
Set localUserinfoes = new HashSet();
localUserinfo.setUseraccesstable(localUser);
localUserinfoes.add(localUserinfo);
localUser.setUserinfos(localUserinfoes);
localSession.saveOrUpdate(localUser);

localSession.saveOrUpdate(localUserinfo);

tx.commit();
}

The code fails calling localSession.saveOrUpdate(localUserinfo); and
the exception says:
"Field 'fkIdUserAccess' doesn't have a default value"
The instruction localSession.saveOrUpdate(localUser); works correctly.
It seems that the code that I wrote doesn't link the two entities
through the foreign key fkIdUserAccess.
Where is my error? Is there someone that can help me?
Thanks Giovanni


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 8:01 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
You need to set cascade="save-update" attribute on association from Useraccesstable class to collection of Userinfo classes. With this setting, you doesn't need the second saveOrUpdate() call, new Userinfo class will be saved automatically by the Hibernate cascading mechanizm.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 28, 2006 1:39 pm 
Newbie

Joined: Wed Dec 27, 2006 5:58 am
Posts: 2
Hi,
you are right, now everything works as expected. I omitted example in chapter 21, so a replay RTFM would be appropriated :-)
Thanks again for your very useful help.
Giovanni


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.