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.
|