-->
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: User / role many-to-many association problem
PostPosted: Thu Mar 11, 2004 10:17 am 
Newbie

Joined: Thu Sep 04, 2003 4:59 am
Posts: 5
Hi,

We are using Hibernate 2.0.3 with Hypersonic 1.7.2 database.
We try representing a typical User / Role association with following mapping documents.
Role mapping file :

<?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.axway.xpm.xdk.workflow.XpmDKWfwRole" table="XPMDKWFWROLE">
<id name="roleIdTech" column="roleIdTech" type="java.lang.String" unsaved-value="null">
<generator class="uuid.hex">
</generator>
</id>
<property name="roleName" type="java.lang.String"/>
<set name="XpmUserList" cascade="all" table="user_role" inverse="true">
<key column="roleIdTech"/>
<many-to-many class="com.axway.xpm.xdk.workflow.XpmDKWfwUser" column="userIdTech"/>
</set>
</class>

</hibernate-mapping>

User mapping file :

<?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.axway.xpm.xdk.workflow.XpmDKWfwUser" table="XPMDKWFWUSER">
<id name="userIdTech" column="userIdTech" type="java.lang.String" unsaved-value="null">
<generator class="uuid.hex">
</generator>
</id>
<property name="userName" type="java.lang.String"/>
<property name="userPwd" type="java.lang.String"/>
<property name="userType" type="integer"/>
<set name="XpmRoleList" cascade="all" table="user_role">
<key column="userIdTech"/>
<many-to-many class="com.axway.xpm.xdk.workflow.XpmDKWfwRole" column="roleIdTech"/>
</set>
</class>

</hibernate-mapping>


We try to execute the following scenario :
adding four roles
inserting a first user associated with three roles
inserting a second user associated with the same three roles

The last action produces following exception :
com.axway.util.io.XPMException: The user user8 cannot be created : Another object was associated with this id (the object with the given id was already loaded): [com.axway.xpm.xdk.workflow.XpmDKWfwRole#2c902068fb3a12e600fb3a1398fb0003]
at com.axway.xpm.xdk.workflow.XpmDKWfwSession.addUser(XpmDKWfwSession.java:366)
at com.axway.xpm.xdk.workflow.XpmDKWfwSessionManager.addUser(XpmDKWfwSessionManager.java:167)
at com.axway.xpm.xdk.workflow.XpmDKWfwAdmManager.addUser(XpmDKWfwAdmManager.java:370)
at com.axway.xpm.xdk.workflow.XpmDKWfwMainTest.testDeDebug(XpmDKWfwMainTest.java:1318)
at com.axway.xpm.xdk.workflow.XpmDKWfwMainTest.main(XpmDKWfwMainTest.java:64)

For information, the JavaCode using Hibernate API is :
Code:
try
{
sess = sessions.openSession(DBWorkflowManager.getGestionBD(dbUrl).getWorkflowCnx4Tx());

sess.saveOrUpdate(user);
               
sess.flush();
sess.connection().commit();
}
catch (Exception e)
{
   try
   {
      sess.connection().rollback();
   }
   catch (Exception ex)
   {
      // do nothing
   }
   throw new XPMException(e.getMessage());
}
finally
{
   try
   {
      sess.connection().close();
   }
   catch (Exception ex)
   {
      // do nothing
   }
   try
   {
      sess.close();
   }
   catch (Exception ex)
   {
      // do nothing
   }
}


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 10:44 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I think it would be helpful if you show us the code where you add the roles to the user.

James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 11:08 am 
Newbie

Joined: Thu Sep 04, 2003 4:59 am
Posts: 5
Ok, here is source code.

User class :
Quote:
public class XpmDKWfwUser
{
private String userPwd = null;
private String userIdTech = null;
private String userName = null;
private Set XpmRoleList = null;
private int userType = 0; // type de User (simple ou admin)

/**
* Constructs a newly allocated <code>XpmDKWfwUser</code> object.
* The attributes of this class are initialised to <code>null</code>.
*/
public XpmDKWfwUser()
{
}

/**
* Return the user name
*
* @return userName user name
*/
public String getUserName()
{
return userName;
}

/**
* Return the list of the roles associated to this user
*
* @return Set role list
*/
public Set getXpmRoleList()
{
return XpmRoleList;
}

/**
* Sets the userName.
*
* @param userName The user name to set
*/
public void setUserName(String userName)
{
this.userName = userName;
}

/**
* Sets the xpmRoleList.
*
* @param xpmRoleList The Role list to set
*/
public void setXpmRoleList(Set xpmRoleList)
{
XpmRoleList = xpmRoleList;
}

/**
* To get the user technical identifier. It's just use for the user persistence
*
* @return the user technical identifier
*/
public String getUserIdTech()
{
return userIdTech;
}

/**
* Sets the user technical identifier. It's just use for the user persistence
*
* @param rIdTech the role technical identifier
*/
public void setUserIdTech(String uIdTech)
{
userIdTech = uIdTech;
}

/**
* Return the user password
*
* @return userPwd user password
*/
public String getUserPwd()
{
return userPwd;
}

/**
* Sets the user password.
*
* @param uPwd The user password to set
*/
public void setUserPwd(String uPwd)
{
userPwd = uPwd;
}

/**
* Return the user type
*
* @return userType user type {@link com.axway.xpm.xdk.util.XpmDKConstantes.USER_TYPE_ADMIN } for an Administrator
* {@link com.axway.xpm.xdk.util.XpmDKConstantes.USER_TYPE_SIMPLE } for a simple user
*/
public int getUserType()
{
return userType;
}

/**
* Sets the user type.
*
* @param userType user type {@link com.axway.xpm.xdk.util.XpmDKConstantes.USER_TYPE_ADMIN } for an Administrator
* {@link com.axway.xpm.xdk.util.XpmDKConstantes.USER_TYPE_SIMPLE } for a simple user
*/
public void setUserType(int type)
{
userType = type;
}

/**
* Remove a role from the role List depending of this user
*
* @param roleName role to delete
*/
public void removeRole(String roleName)
{
// cf removeUser dans classe XpmDKWfwRole
TreeSet tabRole = new TreeSet(new XpmDKWfwSortRole());

Object[] roleList = XpmRoleList.toArray();

for (int i=0; i<XpmRoleList.size(); i++)
{
if ( ((XpmDKWfwRole)(roleList[i])).getRoleName().equals(roleName) )
{
roleList[i] = null;
}
else
{
tabRole.add(roleList[i]);
}
}

XpmRoleList = tabRole;
}

}


Role class :
Quote:
public class XpmDKWfwRole
{
private String roleName = null;
private String roleIdTech = null;
private Set XpmUserList = null;

/**
* Constructs a newly allocated <code>XpmDKWfwRole</code> object.
* The attributes of this class are initialised to <code>null</code>.
*/
public XpmDKWfwRole()
{
}


/**
* Return the role identifier
*
* @return roleName role identifier
*/
public String getRoleName()
{
return roleName;
}


/**
* Return the list of the users associated to this role
*
* @return Set user list
*/
public Set getXpmUserList()
{
return XpmUserList;
}

/**
* Sets the roleName
*
* @param roleName The roleName to set
*/
public void setRoleName(String roleName)
{
this.roleName = roleName;
}


/**
* Sets the xpmUserList.
*
* @param xpmUserList The User list to set
*/
public void setXpmUserList(Set xpmUserList)
{
XpmUserList = xpmUserList;
}

/**
* To get the role technical identifier. It's just use for the role persistence
*
* @return the role technical identifier
*/
public String getRoleIdTech()
{
return roleIdTech;
}

/**
* Sets the role technical identifier. It's just use for the role persistence
*
* @param rIdTech the role technical identifier
*/
public void setRoleIdTech(String rIdTech)
{
roleIdTech = rIdTech;
}

/**
* Remove a user from the user List depending of this role
*
* @param userName user to delete
*/
public void removeUser(String userName)
{
// si on fait un XpmUserList.remove(user), ca ne fonctionne pas
// car l'id de l'objet present dans la liste n'est pas = a celui qu'on
//passe en param. je dois donc faire une recherche du user dans la liste
// par username, le loader et ensuite l'effacer

/* CE code ne fonctionne pas, le remove ne se fait pas et retourne false
* il va falloir faire une petite bidouille
*
*
for (Iterator userList = XpmUserList.iterator(); userList.hasNext();)
{
XpmDKWfwUser userInList = (XpmDKWfwUser)(userList.next());
if ( userInList.getUserName().equals(userName) )
{
boolean bool = XpmUserList.remove(userInList);

boolean tutu = true;
}
}
*/
TreeSet tabUser = new TreeSet(new XpmDKWfwSortUser());

Object[] userList = XpmUserList.toArray();

for (int i=0; i<XpmUserList.size(); i++)
{
if ( ((XpmDKWfwUser)(userList[i])).getUserName().equals(userName) )
{
userList[i] = null;
}
else
{
tabUser.add(userList[i]);
}
}

XpmUserList = tabUser;
}

}


Main class :
Quote:
TreeSet tabRole = new TreeSet();
user1 = new XpmDKWfwUser();
user1.setXpmRoleList(tabRole);
user1.setUserName(newUserName);
user1.setUserPwd(newUserPwd);
user1.setUserType(newUserType);
persist.saveUser(user1, true);


Where "tabRole" is set with the list of Role objects...

Thanks.


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.