• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException in Hibernate

 
Albin Joseph
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. The problem solved. There was a mis match in the data type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic