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
|