Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Need clarification in Many to one Mapping- Please help.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

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 :



Java code Snippet to insert data into the table:


Error that I get :

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

thanks
Arjun.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the mapping of: <many-to-one class="ValueObjects.StudentData" column="Stud_id" name="studentID" access="field" />
In code base class the declaration would be:

Where I have used the zero element as the primary key of the ValueObjects.StudentData.

Similarly for CourseData.

Then the your code would become

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
thanks for your response. But May be I am dump.
Did you mean i should set an instance of valueObjects.StudentData to the studID. Since studID is of type int. How can i set instance of valueObjects.StudentData.

In the mapping i have removed the access property in the many-to one mapping, since i have proper getter and setter methods now.

Please help me..

Arjun.
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare studID as object of ValueObjects.StudentData and not of int as you have declared in the .hbm file as a ValueObjects.StudentData.
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thanks a ton...
I intended to have that as int only. So I'll change the class property to Java.util.Interger. Hope that will work because of autoboxing.
Not sure if i can mention a primitive type there and not the class. since studID is of int. Just wondering why should be an Integer object created in place of just a primitive.

thanks again
Arjun.
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

It seems that I cant have something like this to mean that studID is of type integer


The studentResult table should always hold the instance StudentData object.

Please correct if i'm wrong.

--Arjun.
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthick,

Hibernate helps you to picture DB in terms of objects. So if ClassA has a reference to ClassB in DB by a key value then in Hibernate terms, ClassA will contain ClassB object where the key of ClassB object would be the referencing key.
The Hibernate Tutorial can be checked for this.
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Reehan

this gives a good start to dive into Hibernate.

--Arjun.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic