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):