-->
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.  [ 5 posts ] 
Author Message
 Post subject: User to UserRole strings in Tomcat
PostPosted: Mon Aug 28, 2006 2:46 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I'm trying to map a user to user_roles though a set of strings. I have a synthetic, generated id in the User entity and this appears to make it impossible to map to the <natural-id>login</value>

Code:
user
=====
id int(11) not null auto_increment,
login varchar(255) not null default '',
email varchar(255) not null default ''

user_roles
====
login varchar(255) not null default '',
role varchar(255) not null default '',


When I attempt to map this using a mapping like:
Code:

<set name="roles" table="user_roles">
  <key column="login" property-ref="login"/>
  <element column="role" type="string" />
</set>


I end up with a ClassCastException where it appears that Hibernate is attempting to work with a java.lang.Integer (perhaps from the id??) instead of the java.lang.String (from login) that I would expect. .

Does anyone have any ideas on this?

Hibernate version: 3.1.3

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="com.syndic.data.core.User" table="users">
 
    <id name="id" column="id" type="int">
      <generator class="identity"/>
    </id>
   
    <natural-id mutable="false">
      <property name="login" column="login" type="string"/>
    </natural-id>
   
    <property name="email" column="email" type="string"/>
   
    <set name="roles" table="user_roles">
      <key column="login" property-ref="login"/>
      <element column="role" type="string" />
    </set>
   
  </class>
 
</hibernate-mapping>


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

Code:
User u = new User();
u.setLogin(login);
u.setEmail(email);
u.getRoles().add("user");
session.save(u);


Full stack trace of any exception that occurs:
Code:
java.lang.ClassCastException: java.lang.Integer
        at org.hibernate.type.StringType.toString(StringType.java:44)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:87)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
        at org.hibernate.persister.collection.AbstractCollectionPersister.writeKey(AbstractCollectionPersister.java:688)
        at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:969)
        at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:28)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
        at $Proxy1.flush(Unknown Source)
        at com.syndic.hibernate.HibernateFactory.processUpdate(HibernateFactory.java:81)
        at com.syndic.data.dao.UserDAO.update(UserDAO.java:55)
        at com.syndic.data.dao.TestUserDAO.testUserDAO(TestUserDAO.java:30)


Name and version of the database you are using:
MySql 4.1.13

The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject: SQL
PostPosted: Mon Aug 28, 2006 2:50 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Here's the output SQL:

Code:
Running com.syndic.data.dao.TestUserDAO
Hibernate:
    insert
    into
        users
        (login, email)
    values
        (?, ?)
Hibernate:
    insert
    into
        user_roles
        (login, role)
    values
        (?, ?)


Top
 Profile  
 
 Post subject: Multiple Roles?
PostPosted: Mon Aug 28, 2006 7:50 pm 
Newbie

Joined: Fri Aug 25, 2006 7:47 pm
Posts: 2
Location: Mexico City
What I dont understand is why do you need a Bag for the role?? does a User has multiple roles??

What I use in my aplication is:

Code:
User u = new User();
u.setLogin(login);
u.setPassword(pwd);
session.save(u);

UserRole ur = new UserRole();
ur.setLogin(login);
ur.setRole("ROLE_NAME");
session.save(ur);


And when I use the container based authentification with those tables, there's no problem.

Hope it helps...

_________________
"It is impossible to make anything foolproof because fools are so ingenious"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 4:02 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Thanks for the response.

My problem is that the users DO have multiple roles. For example, an ADMIN can also be an EDITOR but not all EDITORS are ADMINs and not all ADMINs are EDITORs. From design perspective I wanted to allow acces to areas of the web application based on the existence of a role.


Top
 Profile  
 
 Post subject: Help
PostPosted: Thu Nov 23, 2006 1:09 pm 
Beginner
Beginner

Joined: Thu May 05, 2005 4:49 pm
Posts: 30
Did you fix this problem? I'm running into the same thing and none of the related posts in this forum have an answer. Let's get it posted somewhere!


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