Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0
Mapping documents:
<hibernate-mapping
>
<class
name="com.stufftolet.model.businessobject.Users.User"
table="Users"
>
<cache usage="read-write" />
<id
name="id"
column="USERID"
type="java.lang.String"
length="20"
unsaved-value="null"
>
<generator class="assigned">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set
name="roles"
table="user_role"
lazy="false"
cascade="save-update"
sort="unsorted"
>
<key
column="id"
>
</key>
<many-to-many
class="com.stufftolet.model.businessobject.Role"
column="rolename"
outer-join="auto"
/>
</set>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public void saveUser(User user) {
getHibernateTemplate().saveOrUpdate(user);
}
public void removeUser(String userid) {
User user = getUser(userid);
getHibernateTemplate().delete(user);
}
Name and version of the database you are using:MySQL 4.1.11
Hi guys,
I got a User class, which has many-to-many relationship with it role and the java file is like below:
public class User implements Serializable{
protected String id;
protected Set roles = new HashSet();
/**
* @return Returns the id.
* @hibernate.id column="USERID"
* unsaved-value="null"
* generator-class="assigned"
* length="20"
* unique="true"
*/
public String getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(String id) {
this.id = id;
}
/**
* Returns the user's roles.
*
* @return Set
*
* @hibernate.set table="user_role" cascade="save-update" lazy="false"
* @hibernate.collection-key column="id"
* @hibernate.collection-many-to-many class="com.stufftolet.model.businessobject.Role"
* column="rolename"
*/
public Set getRoles() {
return roles;
}
/**
* Sets the roles.
*
* @param roles The roles to set
*/
public void setRoles(Set roles) {
this.roles = roles;
}
/**
* Adds a role for the user
*
* @param rolename
*/
public void addRole(Role role) {
getRoles().add(role);
}
}
I am able to add the user role when inserting a new user. But I got a problem of removing and updating the user role. Pls advice, Thanks !
regards,
Mark