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
|