-->
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.  [ 2 posts ] 
Author Message
 Post subject: java.lang.ClassCastException: java.lang.String while saving
PostPosted: Tue Jun 26, 2007 11:27 am 
Newbie

Joined: Tue Jun 26, 2007 4:07 am
Posts: 5
Hi ,
I am getting ClassCastException whenever i try to save one of my Object.

I have checked my Database tables , Classes Definition as well as mapping definition all have similar "types" for all the properties/columns still it is giving this exception .

I checked the log created at server it is also generating the right queries and binding with correct type , still it is failing somewhere .
I'd be very thanful if anyone can tell me where it is failing and how to correct it .

About my Code --

A simple program that has Student Class and Course Classmapped with Student table and Course Table Respectively .

A student can belong to many courses so a table student_course has been made for this association .

I am using Hibernate3 and JBoss AS .

Please help me !!!

Following is the code that is failing-(raising exception on tx.commit();)

Student student = new Student();
student.setFirstName(request.getParameter("firstName"));
student.setLastName(request.getParameter("lastName"));
//student.setDob(null);
student.setAddress(request.getParameter("address"));
String[] courses = request.getParameterValues("courses");
HashSet coursesForStudent = new HashSet();
for(int i = 0 ; i < courses.length; i++)
{

Course course = (Course) session.load(Course.class, courses[i]);
coursesForStudent.add(course) ;
}
student.setCourses(coursesForStudent) ;
session.save(student);
//sessiongetTransaction().commit();
tx.commit(); // Faling on this line

Following is the part of Log generated at server here the biding types are shown correctly as org.hibernate.type.LongType ---
------------------------------------
2007-06-26 20:14:43,504 DEBUG [org.hibernate.jdbc.AbstractBatcher] closing statement
2007-06-26 20:14:43,504 DEBUG [org.hibernate.persister.collection.AbstractCollectionPersister] Inserting collection: [sis.Student.courses#34]
2007-06-26 20:14:43,504 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2007-06-26 20:14:43,504 DEBUG [org.hibernate.SQL] insert into STUDENT_COURSE (STUDENTROLLNO, COURSEID) values (?, ?)
2007-06-26 20:14:43,504 INFO [STDOUT] Hibernate: insert into STUDENT_COURSE (STUDENTROLLNO, COURSEID) values (?, ?)
2007-06-26 20:14:43,504 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
2007-06-26 20:14:43,504 DEBUG [org.hibernate.type.LongType] binding '34' to parameter: 1
2007-06-26 20:14:43,504 DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 2
2007-06-26 20:14:43,504 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/BankManagementWeb].[StudentManager]] Servlet.service() for servlet StudentManager threw exception
java.lang.ClassCastException: java.lang.String
at org.hibernate.type.LongType.set(LongType.java:40)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
-------------------------------------------------------------------------



Following is the mapping for the object that i am trying to save ---

<hibernate-mapping>
<class name="sis.Student" table="Student">
<id name="rollNo" column="rollNo" type="long">
<generator class="sequence">
<param name="sequence">student_rollNo_sequence</param>
</generator>
</id>

<property name="firstName"/>
<property name="lastName"/>
<property name="dob" type="date"/>
<property name="address" />

<set name="courses" table="STUDENT_COURSE">
<key column="STUDENTROLLNO"/>
<many-to-many column="COURSEID" class="sis.Course" />
</set>
</class>
</hibernate-mapping>

Following is the mapping for Course Class --

<hibernate-mapping>

<class name="sis.Course" table="Course">
<id name="id" column="id" type="long">
<generator class="sequence">
<param name="sequence">course_id_sequence</param>
</generator>
</id>

<property name="name"/>
</class>

</hibernate-mapping>

Thanks in advance for your help .

Best Regards ,
Chandan Ahuja


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:47 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
What is your db sequence type? It seems like those sequences are string typed although your ids are long ones. Maybe try changing those sequence to long or changing the id types.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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