-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to get the real child object from the parent object
PostPosted: Sun Feb 05, 2006 10:16 pm 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
Hello, everybody

I programmed a polymorphism parent-children Pojos like Parent, Child1 extends Parent, Child2 extends Parent, Child3 extends Parent with a table per subclass way. I need to use lazy fetching to all these objects.

I get a list of Parent, and I want to iterator them and get each real child object and to do some operations. It's obviously wrong as:
Child1 child = (Parent) parent;
because parent is a proxy object of Parent, but not Child1.

I really need the exact object of Child1/Child2/Child3 from the list, how can I get it from the parent object?

Should I load each Child class again respectively with the id of parent?

Thank you so much,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 1:42 am 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
I found a class org.hibernate.engine.PersistenceContext, it has a method:

public Object unproxy(Object maybeProxy) throws HibernateException

But seems I can't get an instance of PersistenceContext.

Any suggestion?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 2:44 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Cast it to HibernateProxy.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 3:42 am 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
baliukas wrote:
Cast it to HibernateProxy.


Thank you very much.

And so my codes will be bond to Hibernate here, right?

Ok, if there's no better solution, I'll take this one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 4:02 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Implement static helper.

static Object narrow(Object o){

if(o instanceof HibernteProxy){
return ((HibernteProxy)o).getLazyInitializer().getImplementation();
}else {
return o;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 1:42 pm 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
baliukas wrote:
Implement static helper.

static Object narrow(Object o){

if(o instanceof HibernteProxy){
return ((HibernteProxy)o).getLazyInitializer().getImplementation();
}else {
return o;
}

}



For Hibernate3, it's getHibernateLazyInitializer(). So it is:

private Object narrow(Object object){
if(object instanceof HibernateProxy){
return ((HibernateProxy)object).getHibernateLazyInitializer().getImplementation();
}else {
return object;
}
}

I tested it and it work very well. Thank you very much.


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