-->
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.  [ 1 post ] 
Author Message
 Post subject: InstanceOf, Casting issues with proxies find solution
PostPosted: Tue Nov 26, 2013 4:28 am 
Newbie

Joined: Tue Nov 26, 2013 3:59 am
Posts: 1
Hello,

I'm working on a project where entities inheritance is frequently used, the business logic depends on checking the entity's instance and depends on casting. There are switches like:
if (entity instanceof Location) then ...
else if (entity instanceof TransportUnit) then ...

and castings like:
location = (Location) entity;

The problem is that when the lazy loading was enabled for OneToOne, ManyToOne, DAOs started to return proxies and also proxies are also inside entities lists(like @OneToOne getters).

I know that it is possible to use this in our getters:
if (entity instanceof HibernateProxy)
{
Hibernate.initialize(entity);
entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
}
But this would have to be added not only to @ManyToOne, @OneToOne but also to @OneToMany, @ManyToMany (to unproxy when we iterate through the lists/sets) and we'll also have to do this in all the DAOs methods, or use some AOP for this - perhaps ideally directly inside some methods inside EntityManager(project uses JPA) or somewhere inside Hibernate. Unfortunately this seems to be risky, and performance expensive.

Are there any other solutions that would permit freely using instanceof and casting in the project, except changing the relations back to eager and without using Hibernate specific @LazyToOne(LazyToOneOption.NO_PROXY) in combination with @Proxy(lazy = false) annotations?

I do not mind if the entities will implement or extend some interface or abstract class (ideally hibernate independent).
Thank you in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.