-->
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.  [ 10 posts ] 
Author Message
 Post subject: java.lang.ClassCastException: java.lang.Long
PostPosted: Wed Mar 15, 2006 12:24 am 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
Hibernate version:3.1
Database:MySQL 5


Hi,

I have been struggling with this error for a whole day - am becoming more familiar with Hibernate source than I wanted to (an excellent piece of software, though).

I am getting a ClassCastException when try to reassociate a detached instance of my "User" object. It has to do with the way the Role object is associated - I am not using the default ID association, but rather, its username property.

My mapping files and exception stack traces are below. Any help would be hugely appreciated.

Thanks.

Lee.

User Mapping:

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

<hibernate-mapping package="com.fsg.mainsite.common">
<class name="User" table="USER" dynamic-update="true" dynamic-insert="true" lazy="false">
<id name="id" type="java.lang.Long" column="ID">
<generator class="native"/>
</id>
<property name="username" column="USERNAME" type="java.lang.String" length="40" not-null="true"/>
<property name="password" column="PASSWORD" length="40" not-null="true"/>
<property name="firstName" column="FIRST_NAME" length="40" not-null="true"/>

<set name="roles" table="USER_ROLE" cascade="none" lazy="false" fetch="join">
<key column="USERNAME" property-ref="username"/>
<many-to-many class="Role" column="ROLE_ID"/>
</set>
</class>
</hibernate-mapping>

Role mapping:

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

<hibernate-mapping package="com.fsg.mainsite.common">
<class name="Role" table="ROLE" dynamic-update="true" dynamic-insert="true" lazy="false">
<id name="id" type="java.lang.Long" column="ID"/>
<property name="name" column="NAME" length="40" not-null="true"/>
<property name="description" column="DESCRIPTION" length="200" />
</class>
</hibernate-mapping>

Stack Trace (and log output excerpt):

DEBUG opened session at timestamp: 4679251227717632
DEBUG updating detached instance
DEBUG updating [com.fsg.mainsite.common.User#1]
java.lang.ClassCastException: java.lang.Long
at org.hibernate.type.StringType.toString(StringType.java:44)
at org.hibernate.type.NullableType.toLoggableString(NullableType.java:169)
at org.hibernate.pretty.MessageHelper.collectionInfoString(MessageHelper.java:187)
at org.hibernate.event.def.ReattachVisitor.removeCollection(ReattachVisitor.java:60)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:46)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:267)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:586)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:574)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:566)
at com.fsg.mainsite.DataInserter.test5(DataInserter.java:193)
at com.fsg.mainsite.DataInserter.main(DataInserter.java:72)

Java Code:

User user = DaoFactory.getUserDao().findByUsername("lee");
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();

Session session = HibernateUtil.getSession();
session.update(user); // Fails on this line !!!
System.out.println("user=" + user);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 1:37 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
And what does the POJO look like ?

Is field "id" a long type ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 4:13 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
Quote:
I am not using the default ID association, but rather, its username property.

In the Mappings:
Quote:
Code:
User: <id name="id" type="java.lang.Long" column="ID">
Role:  <id name="id" type="java.lang.Long" column="ID"/>


You can only have foreign key references via the primary key, not via the username, I think.


Top
 Profile  
 
 Post subject: POJOs
PostPosted: Wed Mar 15, 2006 6:24 am 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
THe POJOs are as follows. Although username is a unique key, the primary key is Long for both Role and User. I hope that I don't have to do the association by primary key, because the J2EE security framework requires that I have a "USERNAME" field in teh ROLE table. If there is a way to have that extra field in the role table, that should work as well.

public class User
{
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
private Set<Role> roles;

public User()
{
roles = new HashSet<Role>();
}

public boolean equals(Object obj)
{
if (obj == null)
return false;
else
{
User otherUser = (User)obj;
if (this.getUsername().equals(otherUser.getUsername()))
return true;
}
return false;
}

public int hashCode()
{
return getUsername().hashCode();
}

}



public class Role
{
private Long id;
private String name;
private String description;

Role()
{

}

public Role(Long id, String name)
{
super();
this.id = new Long(id);
this.name = name;
}

public boolean equals(Object o)
{
boolean result = (o != null && o instanceof Role && name.equals(((Role)o).name));
return result;
}

public int hashCode()
{
return name.hashCode();
}

public String getDescription()
{
return description;
}

public void setDescription(String description)
{
this.description = description;
}

public Long getId()
{
return id;
}

protected void setId(Long id)
{
this.id = id;
}

public String getName()
{
return name;
}

protected void setName(String name)
{
this.name = name;
}
}


Top
 Profile  
 
 Post subject: ClassCastException - appears to be a bug
PostPosted: Fri Mar 17, 2006 1:53 pm 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
I posted this a few days ago, and have included all details relating to the "bug". Nobody has come up with an answer, so I can assume this is a bug. There are a couple of workarounds but they're quite ugly. Does anyone know how I can determine if this is indeed a bug, and if so, when it will be fixed?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
If your posting (or a question you are referring to) was not answered by anybody, the possible reasons are:

- http://www.hibernate.org/ForumMailingli ... AskForHelp
- You did not submit enough information
- Nobody knows the answer or has the free time to answer

What you can do now:

- Do the things listed in After Posting
- Add missing and/or more information
- Consider commercial support for guaranteed expert response times

This is a high-traffic forum run by volunteers with hundreds of postings made every day. The community works because people try to help others in their free time. Nobody is paid for this service or has to pay.

You should not expect a timely response and you should not rely on a public community forum for critical cases.

All community members should respect the rules of this forum and treat others like they would prefer to be treated.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Commercial support
PostPosted: Fri Mar 17, 2006 3:35 pm 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
Can you recommend the "best" (price+service) place to get commercial support?

Thanks.

Lee.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 4:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
http://www.hibernate.org/148.html

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: still a problem in 3.2.3 - below is reference to bug
PostPosted: Tue Apr 10, 2007 7:03 pm 
Newbie

Joined: Tue Apr 10, 2007 5:23 pm
Posts: 2
http://opensource.atlassian.com/project ... e/HHH-2052

Please vote for it!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 7:54 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Different problem, I think.

Code:
<set name="roles" table="USER_ROLE" cascade="none" lazy="false" fetch="join">
   <key column="USERNAME" property-ref="username"/>
   <many-to-many class="Role" column="ROLE_ID"/>
</set>


Something seems very odd when your unique String is being used as a foreign key to a Long.


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