aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope." Watch "Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope." New topic
Author

Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.

Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Hi All
I am new to hibernate.
I am trying to do many-to-one mapping in hibernate.
I am using 2 tables :

1) UserTypes(usertypesid is primary key)
2) Users (userid is primary key and usertypesid is foreign key from usertypes table.)


And for the users table is as follows:




I have been able to insert the records into user tables by mapping successfully but the problem comes when i try to retrieve the records from userypes table..
It also retrieve the records from the user table i dnt knw y??

I have tried all the forums and articles now this is my last hope. Please help me if anybody has any idea.
The hql for usertypes is "

And this is generating the sql like the followings:


I am not able to understand y its also showing the select sql from user table?
Anybody has any idea advise my..
Thanks a lot..


Khushwinder
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You don't seem to have a proxy interface defined for either class. Hibernate needs a proxy to implement lazy initialization, without it the lazy="true" attributes of your association mapping are meaningless (true is the default anyway, so this attribute is unnecessary unless you want to disable lazy initialization for this particular association).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Thanks a lot Paul for your feedback.
According to you:
1) i need to do implement proxy interface for my classes
2) I need to give lazy="false" in spite of lazy="true"

Would you like to add something for further explain your answer?
I will highly appreciate your valuable suggestions..

Thanks once again.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


According to you:
1) i need to do implement proxy interface for my classes

Yes, though the normal route is to let the class itself can behave as its own proxy. Add a proxy attribute to your class mapping (see the Hibernate documentation if you)

2) I need to give lazy="false" in spite of lazy="true"

No. All associations in Hibernate are by default lazy. If you don't have a proxy class to implement this lazy initialization though then the lazy attribute is meaningless. You can use lazy="true" for clarity's sake (which is what I tend to do) but it is not necessary.
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Thanks for reply Paul.
I am doing exactly as per your saying.
i have given proxy as same as the class name and lazy="true".
But the output is still same.
I also have read 100s of examples on the net they have not given that
the proxy attribute is necessary.
I am unable to figure out the problem with my code.
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
i have solved it by myself after 3 weeks
if anybody will encounter this in future you can ask me at chahal_khushwinder@yahoo.co.in
thanks a lot
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by khushwinder khushwinder:
i have solved it by myself after 3 weeks
if anybody will encounter this in future you can ask me at
thanks a lot


Or you can post it in the forum. It is probably best not to post your email address in a forum, because you will most likely receive more spam.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.
 
Similar Threads
Hibernate Issue
Hibernte will delete rows from a table, but it won't insert rows
problem with hibernate
Hibernate mapping
Hibernate Repeated column mapping problem