-->
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.  [ 9 posts ] 
Author Message
 Post subject: New to hibernate, need help with references
PostPosted: Fri Oct 27, 2006 5:08 pm 
Newbie

Joined: Sun Oct 22, 2006 7:11 am
Posts: 7
Hi,

Im new to Hibernate and have a are using JBoss (to authorize login). I have 3 tables:


db_user
USER_NAME(primary key)
USER_PASSWORD

db_role
ROLE_NAME(primary key)

db_role_user
USER_NAME(primary key)
ROLE_NAME(primary key)


How would I map (write my *.hbm.xml) my objects into these tables (User and Role)? This is how a user and role is stored (A user is created with a static role). I have no actual use of the table db_role but the db_role_user is used by JBoss in authentication.


Role role = new Role();
role.setName("user");

User user = new User();
user.setName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setRole(role);

Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try
{
tx = session.beginTransaction();
session.save(user);
tx.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
finally
{
InitSessionFactory.close();
}


Kind regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 3:26 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Since each user has only one role, you have a many-to-one association here. Can you get rid of the db_role_user table?

I would simply use:
db_role: ROLE_NANE
db_user: ROLE,NAME,PWD

The user.hbm file would include the following property mapping:
<many-to-one name="role" column="ROLE_ID" class="Role" not-null="true"/>

If you also need to access all users from a single role object (i.e. role.getUsers()), please specify exactly how you plan on doing this, because the required mapping may differ, depending on the way you wish to access the users.

Question you need to answer for yourself: If a role record is removed, what happens to all users with that role?

(p.s. please rate if this was helpful :-) )


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 10:18 am 
Newbie

Joined: Sun Oct 22, 2006 7:11 am
Posts: 7
Hi,

Many thanks for the help!

I cant get rid of the db_role_user, this is used by JBoss to authorize the user login (to get the role), but I can get rid of the db_role.

This is actually a many-to-many relation since a user can have many roles (in theory, not used at the moment) and a role can have many users. The relation table is the db_user_role.

My problem is that I dont want to save a role that already excist. I want to save the user in the db_user table and at the same time save a record in the db_user_role table (there is actually no need for the db_role table).

What I would like to do is save users in db_user and at the same time save their role in the db_user_role.

Kind regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 11:30 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I think I now understand....

First, disable auto-session-close in the .cfg file:
<property name="transaction.auto_close_session">false</property>

Now, what you need is this:
Do not CREATE new role objects. Instead, LOAD the relevant one from the db:

//begin tx...
Role role = (Role)session.get(Role.class, "user");
// end tx...
User user = ... create a new user and call setRole(role);
// finally, save the user using a new tx in the SAME session!
// finally close the session using: session.close

This will ensure that the used role is the existing one. Note: you can do everything in a single tx as well, but usually from my experience, the part where you load the role and the part where you store the data is done separately in the application...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 12:11 pm 
Newbie

Joined: Sun Oct 22, 2006 7:11 am
Posts: 7
Hi,

Again, thank you, this is done. I have another problem in saving the user. This is a class cast exception.

My code

//My code to save..
tx = session.beginTransaction();
User user = new User();
user.setName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
Role role = (Role)session.get(Role.class, "user");
user.setRoles(role);
session.save(user);
tx.commit();

//My User class
..
private Vector roles= new Vector();
..
public void setRoles(Role role)
{
this.roles.add(role);
}
..
public Vector getRoles()
{
return this.roles;
}


My mapping hbm.xml

<hibernate-mapping>
<class name="se.tesella.persistent.Role" table="db_role_user">
<id name="name" column="ROLE_NAME" type="java.lang.String"/>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="se.tesella.persistent.User" table="DB_USER">
<id name="name" column="USER_NAME" type="java.lang.String"/>
<property name="password" column="USER_PASSWORD" type="java.lang.String" />
<set name="roles" table="db_role_user" cascade="all">
<key column="USER_NAME"/>
<one-to-many class="se.tesella.persistent.Role"/>
</set>
</class>
</hibernate-mapping>


This results in a class cast exception (java.lang.ClassCastException). I've tried Array and Collection aswell but with the same result. Could you se what Im doing wrong?

Kind regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 4:55 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Please provide the entire stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 1:41 pm 
Newbie

Joined: Sun Oct 22, 2006 7:11 am
Posts: 7
Here it is, again thanks for all your help:


java.lang.ClassCastException: [Lse.tesella.persistent.Role;
org.hibernate.type.SetType.wrap(SetType.java:39)
org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:84)
org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:51)
org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:98)
org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
org.hibernate.event.def.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:360)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:537)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:525)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:521)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
$Proxy59.save(Unknown Source)
se.tesella.servlet.Register.doPost(Register.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)



Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 9:57 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
which line throws the exception? session.get(Role.class... ) ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 12:56 pm 
Newbie

Joined: Sun Oct 22, 2006 7:11 am
Posts: 7
Hi,

Problem solved. I changed Vector to Set.

Thanks for your help!

Best regards


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