-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy initialization does not work with @ManyToOne
PostPosted: Tue Jun 02, 2009 5:38 am 
Newbie

Joined: Thu Aug 07, 2008 12:03 pm
Posts: 16
Hi!
I'm running into a problem that I honestly don't know how to work around. I would really apreciate any hint you guys can give me.

I have to entities. Town and Country.
Country is straight foward, but Town has a many-to-one relation to country. (a town belongs to a country, and a country can be associated to many towns).
I've decided to make the relation lazy, because most of the times I don't need to countries when I query the towns. But from time to time I will require them.

This is the model simplified:

Code:
@Entity
@Table(name="COUNTRIES")
public class Country {
   @Id
   private Long id;
   @Column(nullable=false)
   private String name;
}


Code:
@Entity
@Table(name="TOWNS")
public class Town{
   @Id
   private Long id;
   @Column(nullable=false)
   private String name;
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(nullable = false, name = "country_id")   
   private Country country;
}


Now, when I want to retrieve towns with countries I do something like this:

Code:
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false)   
public Town getTownWithCountry(final Long id){   
    Town town = townDAO.find(id);
    org.hibernate.Hibernate.initialize(town.getCountry());
    return town;
}


Aja, that code runs with no errors. But after, if I try to retrieve any field from the country (like country.getid()) everything is null !!! I mean, the fields are null, but the country itself is not.

I debugged it, and this is what I found:


Code:
country Country_$$_javassist_10  (id=100)   
   handler   JavassistLazyInitializer  (id=111)   -- this is an object
   id   null   
   name   null   


Uhmm... that's a proxy I guess, but not very useful to me.

What am I doing wrong? Does it have to do with the fact that the transaction is in SUPPORT?

I would really appreciate any help here. I can't find documentation about this.

Thanks!


Top
 Profile  
 
 Post subject: Re: Lazy initialization does not work with @ManyToOne
PostPosted: Wed Jul 08, 2009 5:45 am 
Newbie

Joined: Thu Aug 07, 2008 12:03 pm
Posts: 16
Bump!

I would really appreciate any help in this problem I'm facing. I really don't know what to do.

In the example above, If I perform a:

Code:
List<Town> towns= entityManager.createQuery("SELECT t FROM Town AS t").getResultList();


I get a list of towns, but If I try this:

Code:
for (Town t : towns) {
  System.out.println(t.getCountry.getName());
}


I get "null" printed. The proxy is not doing anything and no hits are done to the database to retrieve the countries.
I tried to debug that code and there isn't any code that intercepts the "getCountry().getName()".

However, If I try this:

Code:
for (Town t : towns) {
  System.out.println(((HibernateProxy)p.getCountry()).getHibernateLazyInitializer().getImplementation());
}


it works, now a hit is done to the database to retrieve each country, which is correct.

But why is not the proxy working? Could be due to my hibernate configuration? Could be due to any AspectJ ??

Thanks for any help.


Top
 Profile  
 
 Post subject: Re: Lazy initialization does not work with @ManyToOne
PostPosted: Wed Jul 08, 2009 8:15 am 
Newbie

Joined: Thu Aug 07, 2008 12:03 pm
Posts: 16
Hi, I found where the problem was (for anyone that might be interested).

My pojos where like this:

@Entity
@Table(name="COUNTRIES")
public class Country {
@Id
private Long id;
@Column(nullable=false)
private String name;

private final getId(){
return id;
}

private final getName(){
return name;
}
}

If you pay attention you'll notice that the getters are FINAL. I was using a tool that automatically generates the getters/setters and I never stop to check them. Big mistake; if the methods are final Javassist can't override them.

I hope my silly mistake helps anyone.


Top
 Profile  
 
 Post subject: Re: Lazy initialization does not work with @ManyToOne
PostPosted: Fri Mar 05, 2010 11:39 am 
Newbie

Joined: Mon Jul 20, 2009 5:09 pm
Posts: 16
Thanks for this hind.
THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU. THANK YOU.


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