-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate bytecode provider stripping annotations
PostPosted: Fri Jun 18, 2010 5:00 pm 
Newbie

Joined: Fri Jun 11, 2010 3:52 pm
Posts: 4
Using Hibernate 3.5..

I am seeing an issue when using standard hibernate sessions that after doing a load() the proxied class loses all information about declared annotations..

For example..
Code:
User customer1 = (User) HibernateUtil.getCurrentSession().load(User.class, id);
Class class1 = customer1.getClass();
class1.getDeclaredAnnotations().length //always 0


However if you use the EntityManager
Code:
EntityManager em = getEMF().createEntityManager();
EntityTransaction tx = em.getTransaction();
User customer2 = em.find(User.class, id);
Class class2 = customer2.getClass();
class2.getDeclaredAnnotations().length //has visibility to correct annotations


this seems to be the case regardless if you use javassist or clib as your bytecode provider

I cant imagine this is intended behavior. It becomes particularly problematic when working with other packages which use or expect annotations. An entity object looked up via hibernate session is now unusable if any downstream processing needs to use annotations provided on the entity class. I am having particular issues with RestEasy , but i imagine this would be a problem with just about any Jaxb marshaling which is expecting class annotations..

I realize i may have to make the final switch over to EntityManager, but thats not the point. Am I missing something or has this always been a problem with Hibernate?

thanks


Top
 Profile  
 
 Post subject: Re: Hibernate bytecode provider stripping annotations
PostPosted: Fri Jun 18, 2010 6:28 pm 
Newbie

Joined: Fri Jun 11, 2010 3:52 pm
Posts: 4
Found a nifty utility method..

http://stackoverflow.com/questions/2216547/converting-hibernate-proxy-to-real-object

Code:
public static <T> T initializeAndUnproxy(T entity) {
    if (entity == null) {
        throw new
           NullPointerException("Entity passed for initialization is null");
    }

    Hibernate.initialize(entity);
    if (entity instanceof HibernateProxy) {
        entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer()
                .getImplementation();
    }
    return entity;
}


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