Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3
Hi
I'm a newbee to Hibernate. I'm trying my first sample in the Hibernate for simple CURD operation. I face a problem while inserting data into the table that has many-one mapping. Please help to resole this. I'm using Tomcat6 and Java 1.5 with hibernate3.
Table Structure:
Code:
CREATE TABLE STUDENT_DETAILS
(
Stud_id int,
CONSTRAINT stud_pk_constr PRIMARY KEY (Stud_id),
);
CREATE TABLE COURSE_DETAILS
(
Course_id int,
CONSTRAINT course_pk_constr PRIMARY KEY (Course_id),
);
CREATE TABLE STUDENT_RESULT
(
Stud_id int,
Course_id int,
Attempt_number int,
CONSTRAINT stud_result_pk_constr PRIMARY KEY (Stud_id,Course_id,Attempt_number),
CONSTRAINT fk_stud_id FOREIGN KEY (Stud_id) REFERENCES STUDENT_DETAILS(Stud_id),
CONSTRAINT fk_course_id FOREIGN KEY (Course_id) REFERENCES COURSE_DETAILS(Course_id),
);
Mapping documents:
Code:
<class name="ValueObjects.StudentResult" table="STUDENT_RESULT">
<id column="Attempt_number" name="attemptNumber" type="int">
<generator class="assigned"/>
</id>
<many-to-one class="ValueObjects.StudentData" column="Stud_id" name="studentID" access="field" />
<many-to-one class="ValueObjects.CourseData" column="Course_id" name="courseID" access="field" />
</class>
Code between sessionFactory.openSession() and session.close():
Code:
StudentResult srdata = new StudentResult();
srdata.setStudentID(Integer.parseInt(studData[0]));
srdata.setCourseID(Integer.parseInt(studData[1]));
srdata.setAttemptNumber(Integer.parseInt(studData[2]));
// Assume session object is created and destroyed correctly.
Transaction tx = session.beginTransaction();
session.save(srdata);
tx.commit();
Full stack trace of any exception that occurs:
Apr 16, 2009 9:50:48 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
SEVERE: IllegalArgumentException in class: ValueObjects.StudentData, getter method of property: id
IllegalArgumentException occurred calling getter of ValueObjects.StudentData.id
Hibernate: update STUDENT_RESULT set Stud_id=?, Course_id=?, Score=?, Course_result=? where Attempt_number=?
Apr 16, 2009 9:50:48 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
SEVERE: IllegalArgumentException in class: ValueObjects.StudentData, getter method of property: id
Apr 16, 2009 9:50:48 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of ValueObjects.StudentData.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1997)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2371)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at Helper.JHelper.destroySession(JHelper.java:34)
at Helper.JHelper.populateStudentResultData(JHelper.java:166)
at tester.populateStudentResult(tester.java:51)
at tester.main(tester.java:25)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 22 more
Name and version of the database you are using:Mysql5.1The generated SQL (show_sql=true):yesDebug level Hibernate log excerpt:defaultPlease help me to resolve this.
thanks
Arjun,[/code]