File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes ClassCastException in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "ClassCastException in Hibernate" Watch "ClassCastException in Hibernate" New topic
Author

ClassCastException in Hibernate

Albin Joseph
Greenhorn

Joined: Jan 22, 2005
Posts: 21
hi

I am new to hibernate. When I am trying to insert a record to the database. I am getting an exception. Below is the stack trace of my exception.

Hibernate: insert into USERS (LOGON_ID, PASSWORD, RESETTED, WRNGCOUNT, LOCKED, USERS_ID) values (?, ?, ?, ?, ?, ?)
java.lang.ClassCastException
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:60)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(Abst
ractEntityPersister.java:1826)

Here USERS_ID is my primary key and is of type long. Below is my mappign file.

<?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.aufond.data.table.User" table="USERS">
<id name="init_usersId" type="long" column="USERS_ID">
<generator class="assigned" />
</id>

<property name="logonId" type="java.lang.String">
<column name="LOGON_ID" />
</property>

<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="resetted" type="boolean">
<column name="RESETTED" />
</property>
<property name="wrongCount" type="int">
<column name="WRNGCOUNT" />
</property>

<property name="locked" type="boolean">
<column name="LOCKED" />
</property>
</class>
</hibernate-mapping>



What is wrong with my code. Please help me.


Java source code examples | Java coding community.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi

in hibernate Types (IntegerType, ....) are used to set values to a PreparedStatement to persist an object and to read values from a ResultSet to transform a persistet object back to a java object.

your stacktrace

Hibernate: insert into USERS (LOGON_ID, PASSWORD, RESETTED, WRNGCOUNT, LOCKED, USERS_ID) values (?, ?, ?, ?, ?, ?)
java.lang.ClassCastException
at org.hibernate.type.IntegerType.set(IntegerType.java:41)


indicates that there is a problem when inserting (that is: creating) a user object. since it happens in the IntegerType i would guess (can you post the User class) that there is a missmatch of types: in your mapping you specify "int" (that's why IntegerType is used) but the field in your User class has another (java) type (e.g. long, short, byte) ?

btw: often you do not need to specify the type attribute, hibernate picks reasonable valueas for default (e.g. String, int, ...).


if you could post the User class....?


pascal
Albin Joseph
Greenhorn

Joined: Jan 22, 2005
Posts: 21
Thanks for your help. The problem solved. There was a mis match in the data type.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ClassCastException in Hibernate