-->
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.  [ 2 posts ] 
Author Message
 Post subject: Need help with composite-id
PostPosted: Thu Dec 07, 2006 9:56 am 
Newbie

Joined: Mon Sep 18, 2006 8:45 am
Posts: 1
Hi, it would be nice if someone help me

Hibernate version:3.1.2

Mapping documents:

<class name="cid.beans.UserRole" table="UserRole">
<synchronize table="USERS" />
<synchronize table="ROLES" />

<composite-id name="id" class="cid.beans.ID">
<key-property name="userID" />
<key-property name="roleID" />
</composite-id>

<property name="asigned" type="java.util.Date" />

<many-to-one name="user" column="userID" insert="false"
update="false" />
<many-to-one name="role" column="roleID" insert="false"
update="false" />
</class>

<class name="cid.beans.User" table="USERS" lazy="false">
<id name="id" column="userID">
<generator class="native" />
</id>

<property name="username" />
<property name="password" />

<set name="userRole" inverse="true">
<key column="userID"/>
<one-to-many class="cid.beans.UserRole"/>
</set>
</class>

<class name="cid.beans.Role" table="ROLES">
<id name="id" column="roleID">
<generator class="native" />
</id>
<property name="rolename" />
<property name="shortname" />

<set name="roleUser" inverse="true">
<key column="roleID" />
<one-to-many class="cid.beans.UserRole" />
</set>
</class>


Code between sessionFactory.openSession() and session.close():

If i save a user with his roles this way, hibernate doesnt generate insert statement by it self. But, when i load a user from db, it generates the select statements for UserRole and Role. Do i have to explicitly save UserRole entity, or am i doing it wrong?

User user = new User();
user.setUsername("sreten");
user.setPassword("260979");

Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();

Role role = (Role) session.get(Role.class, 1);

session.save(user);

UserRole ur = new UserRole(user, role);

// session.save(ur);

user.getUserRole().add(ur);

HibernateUtil.commitTransaction();
HibernateUtil.closeSession();



Name and version of the database you are using:hsqldb 1.8.0.7

Thanks...


Top
 Profile  
 
 Post subject: Re: Need help with composite-id
PostPosted: Tue Dec 12, 2006 9:57 pm 
Newbie

Joined: Wed Dec 06, 2006 6:50 pm
Posts: 7
Hi,

Have you already tried to put cascade="all" in the two <set> mappings?
Like this:

<set ... cascade="all">

Hope this help!
Fred

[quote="sreten"]Hi, it would be nice if someone help me

[b]Hibernate version:3.1.2[/b]

[b]Mapping documents:[/b]

[i]<class name="cid.beans.UserRole" table="UserRole">
<synchronize table="USERS" />
<synchronize table="ROLES" />

<composite-id name="id" class="cid.beans.ID">
<key-property name="userID" />
<key-property name="roleID" />
</composite-id>

<property name="asigned" type="java.util.Date" />

<many-to-one name="user" column="userID" insert="false"
update="false" />
<many-to-one name="role" column="roleID" insert="false"
update="false" />
</class>

<class name="cid.beans.User" table="USERS" lazy="false">
<id name="id" column="userID">
<generator class="native" />
</id>

<property name="username" />
<property name="password" />

<set name="userRole" inverse="true">
<key column="userID"/>
<one-to-many class="cid.beans.UserRole"/>
</set>
</class>

<class name="cid.beans.Role" table="ROLES">
<id name="id" column="roleID">
<generator class="native" />
</id>
<property name="rolename" />
<property name="shortname" />

<set name="roleUser" inverse="true">
<key column="roleID" />
<one-to-many class="cid.beans.UserRole" />
</set>
</class>[/i]

[b]Code between sessionFactory.openSession() and session.close():[/b]

If i save a user with his roles this way, hibernate doesnt generate insert statement by it self. But, when i load a user from db, it generates the select statements for UserRole and Role. Do i have to explicitly save UserRole entity, or am i doing it wrong?

[i] User user = new User();
user.setUsername("sreten");
user.setPassword("260979");

Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();

Role role = (Role) session.get(Role.class, 1);

session.save(user);

UserRole ur = new UserRole(user, role);

// session.save(ur);

user.getUserRole().add(ur);

HibernateUtil.commitTransaction();
HibernateUtil.closeSession();[/i]


[b]Name and version of the database you are using:hsqldb 1.8.0.7[/b]

Thanks...[/quote]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.