-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate initialize doesn't work
PostPosted: Mon Mar 29, 2010 10:30 am 
Newbie

Joined: Mon Mar 29, 2010 6:56 am
Posts: 3
Hi,
I'm new to Hibernate. I have 2 classes which are connected by many-to-one relation and I set lazy loading = true. For example:
public class Domain{
private Long domainId;
private String name;
private Date dateCreated;
private User userCreated;

// getter/setter
}

public class User {
private Long userId;
private String userName;
private String password;

// getter/setter
}

I want to write the Domain object to file with the reference. So I use initialize:
Domain domain = getObject(Domain.class, id);
Hibernate.initialize(domain);
Hibernate.initialize(domain.getUserCreated());
writeToFile(domain);

Then I read object from file:
Object obj = readFromFile();

I want to have the obj like that:
obj.getUserCreated.getClass().getName() is User, not the proxy for User (User$$enhancerByCGILib$$) but somtimes it works, sometimes it doesn't. Could anyone show me how to get the reference? Thank you.


Top
 Profile  
 
 Post subject: Re: Hibernate initialize doesn't work
PostPosted: Mon Mar 29, 2010 10:39 am 
Newbie

Joined: Mon Mar 29, 2010 10:00 am
Posts: 2
Your User object is lazy loaded and out of sync. You should load the complete object (with the association) and put it to the file.
Your: "Sometimes it works":
If your object is complete(not lazy loaded), it works fine.

Solution: Load the complete object, by:

- use EAGER loading instead of LAZY loading
- load the assocation manually from database

Your solution depends on the context and how often you load the object, how many objects you are loading etc.


Top
 Profile  
 
 Post subject: Re: Hibernate initialize doesn't work
PostPosted: Mon Mar 29, 2010 11:30 am 
Newbie

Joined: Mon Mar 29, 2010 6:56 am
Posts: 3
Thanks for your quick reply.
I said that sometimes it works because sometimes obj.getUserCreated.getClass() is User, sometimes it returns User$$enhancerCGILib$$. That's why I don't understand why.
I have to load many domain object and I want to get the user like reference of domain object, not like separate objects. Could you please tell me how to load the complete object like you said? Thanks.


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