-->
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: How to prevent a class from being enhanced $$EnhancerByCGLIB
PostPosted: Fri Sep 08, 2006 7:02 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
I have a ManyToOne Mapping (fetch EAGER), but the class is enhanced using CGLIB.

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="animalId)
public Animal getAnimal() {
...
}


the Animal instance becomes Animal$$EnhancerByCGLIB$$9877ae. As a result, I can not use instanceof or cast, for example,

Animal a = getAnimal();
if (a instanceof Dog) {
....
}

I know it is a Dog, but I can not cast it, like
Dog d = (Dog)a;

How to prevent Animal class from being enhanced? I need to use Animal instead of Animal$$EnhancedByCGLIB$$...

Thanks for help!
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 10, 2006 1:50 am 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
I am not using annotations, so I edit mappings manually.

At the class level, you can use lazy=false in a mapping to prevent Hibernate from using a proxy.

With annotations:
@org.hibernate.annotations.Proxy(lazy = false)

_________________
Denis
Don't forget to rate ... thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 12:53 pm 
Newbie

Joined: Thu Aug 31, 2006 4:05 am
Posts: 5
This is not really an Hibernate problem, but a Java design one. Make use of interfaces.

Code:
class Animal implements IAnimal {
...
}

class Dog extends Animal implements IDog {
...
}

interface IDog extends IAnimal {
...
}



Then your cast will look like this :

Code:

if (animal instanceof IDog) {
   IDog dog = (IDog) animal;
   ...
}


Hope this helps ;)


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.