Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: ManyToOne Lazy/Eager and Inheritance
PostPosted: Tue Nov 27, 2007 11:34 am 
Newbie

Joined: Tue Nov 27, 2007 10:58 am
Posts: 5
Hi everybody,
Im workin on Hibernate version: 3.2.4 (from JBoss 4.2.2.GA)
and I have some problem configuring my entities.

This is my code:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Party

@Entity
public class Person extends Party

@Entity
public class Doctor {
  @ManyToOne(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
  public Party getParty()
}


With this configuration, when i do this:
Code:
Person pers = (Person) doctor.getParty();

everithing is ok!

But when i set party fechtype as LAZY an error occurred:
java.lang.ClassCastException: mypackage.Party_$$_javassist_104

By now I found a workarround to set it LAZY anyway:
instead of this:
Code:
Person pers = (Person) doctor.getParty();


i do this:
Code:
Party party= doctor.getParty();
Person pers = (Person) entityManager.find(Person.class, party.getId());


even if the consequence is this:

WARN [ProxyWarnLog] Narrowing proxy to class myPackage.Person - this operation breaks ==

Can anyone tell me how can I set getParty LAZY and cast without any problem?
Code:
Person pers = (Person) doctor.getParty();


There is anything wrong on my approach?
Any suggestion will be appreciated!

Thanks

Francesco


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 7:08 am 
Newbie

Joined: Mon Dec 10, 2007 7:03 am
Posts: 14
Same problen here: a lazy many-to-one relationship pointing to an abstract class.
Did you find a solution?

http://www.jboss.com/index.html?module= ... c&t=125551


Top
 Profile  
 
 Post subject: ManyToOne Lazy/Eager and Inheritance
PostPosted: Wed Dec 12, 2007 3:53 am 
Newbie

Joined: Tue Nov 27, 2007 10:58 am
Posts: 5
Hi!
I didn't find a solution yet but I found a workarround:

instead of this:
Code:
Person pers = (Person) doctor.getParty();

i do this:
Code:
Party party= doctor.getParty();
Person pers = (Person) entityManager.find(Person.class, party.getId());

Note: Person extends Party, Doctor manyToOne Party


I hope it helps you.
If you find a solution a post would be really appreciated!
Thaks,

Fra


gquintana wrote:
Same problen here: a lazy many-to-one relationship pointing to an abstract class.
Did you find a solution?

http://www.jboss.com/index.html?module= ... c&t=125551


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 4:14 am 
Newbie

Joined: Mon Dec 10, 2007 7:03 am
Posts: 14
Thanks for your solution.

Personnally, I use a "join fetch" when loading the doctor:
Code:
from Doctor as d join fetch d.party where d.id=:doctorId


The problem is described in Hibernate documentation:
http://www.hibernate.org/hib_docs/v3/re ... ng-proxies
But not the solution :-(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 6:19 am 
Newbie

Joined: Tue Nov 27, 2007 10:58 am
Posts: 5
I sometimes did that too...
But I think it's really sad that we have to do the same thing we do programming without entities....

If there is not a solution I'm asking myself why I'm using it... :-/

...let's hope to someone who tell us The solution. :-)


gquintana wrote:
Thanks for your solution.

Personnally, I use a "join fetch" when loading the doctor:
Code:
from Doctor as d join fetch d.party where d.id=:doctorId


The problem is described in Hibernate documentation:
http://www.hibernate.org/hib_docs/v3/re ... ng-proxies
But not the solution :-(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 14, 2007 5:36 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
Please really READ the http://www.hibernate.org/hib_docs/v3/reference/en/html/performance.html#performance-fetching-proxies

Solution is to write and use an INTERFACE for EVERY Entity in your Inheritance-hierarchy. Then you work furthermore only with this interfaces in your code.

Example:

Code:

public Interface IPerson extends IParty {
}

@Entity
@Proxy(IPerson.class)
public class PersonImpl extends PartyImpl implements IPerson {

IPerson person;

@ManyToOne(fetch = FetchType.LAZY, targetEntity=PersonImpl.class)
public IPerson getPerson(){
   return this.person;
}

}


IParty person = this.em.find(PartyImpl.class, id);

(person instanceof IPerson) will return true !!

You can cast IPerson to IParty, but you CANNOT cast IPerson to PersonImpl !


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 12:43 pm 
Newbie

Joined: Tue Nov 27, 2007 10:58 am
Posts: 5
Thanks for your answer Rhodan but I really don't like this solution.
As I explaned in my post I'd like to do this:
Code:
Person pers = (Person) doctor.getParty();

This code works when relationship is set as EAGER but it doesn't work when it's set LAZY.

You're right, I didn't read all the documentation but sorry, what you told me to do is too complex for the problem I have (even if its written on that document).

I don't like that I have to make my design so complex just to set a LAZY relationship.

Rhodan wrote:
Please really READ the http://www.hibernate.org/hib_docs/v3/reference/en/html/performance.html#performance-fetching-proxies

Solution is to write and use an INTERFACE for EVERY Entity in your Inheritance-hierarchy. Then you work furthermore only with this interfaces in your code.

Example:

Code:

public Interface IPerson extends IParty {
}

@Entity
@Proxy(IPerson.class)
public class PersonImpl extends PartyImpl implements IPerson {

IPerson person;

@ManyToOne(fetch = FetchType.LAZY, targetEntity=PersonImpl.class)
public IPerson getPerson(){
   return this.person;
}

}


IParty person = this.em.find(PartyImpl.class, id);

(person instanceof IPerson) will return true !!

You can cast IPerson to IParty, but you CANNOT cast IPerson to PersonImpl !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 12:10 pm 
Newbie

Joined: Thu Jun 12, 2008 1:31 pm
Posts: 2
I have the same problem.
Has anyone found a solution different to write an Interface for Every class in the inheritance?

Thanks
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 1:21 pm 
Newbie

Joined: Tue Nov 27, 2007 10:58 am
Posts: 5
Unfortunately I'm still looking for a better solution.
If you find it could you please post it here?
Tnx!

Fra

scarbo wrote:
I have the same problem.
Has anyone found a solution different to write an Interface for Every class in the inheritance?

Thanks
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:19 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I have seen many people try to do a different implementation, but writing an interface for each class tends to be the easiest way to get around the issues associated with abstract classes in the inheritance tree.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 6:42 pm 
Newbie

Joined: Thu Jun 12, 2008 1:31 pm
Posts: 2
I have tried this workaround and it works for me. I think I'm going to go on this way.
The only problem I have is that we are migrating an existing project form Toplink to Hibernate and we have lots of classes to refactor. But that is my problem.
Thanks


Top
 Profile  
 
 Post subject: Re: ManyToOne Lazy/Eager and Inheritance
PostPosted: Sat Jun 06, 2009 2:18 pm 
Newbie

Joined: Thu Jun 04, 2009 9:53 am
Posts: 1
I have come across the same problem using Hibernate and AMF. my workaround was to write a little class that makes a clone of the original object graph using BeanUtils which then gave me a clean copy of the original object without all the "_$$_" stuff. Seems to be working fine so far.


Top
 Profile  
 
 Post subject: Re: ManyToOne Lazy/Eager and Inheritance
PostPosted: Mon Sep 14, 2009 10:18 pm 
Newbie

Joined: Mon Sep 14, 2009 10:14 pm
Posts: 3
I added the following method to my app's hibernate utils class:

Code:
   
   public static <T> T getProxiedObject(T proxy){
      if ( proxy instanceof HibernateProxy ) {
         return (T) ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().getImplementation();
      }
      return proxy;
   }

Maybe this should be added to the org.hibernate.Hibernate class? (Useful in contexts similar to Hibernate.getClass(proxy))

edit: code formatting


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 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.