-->
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.  [ 4 posts ] 
Author Message
 Post subject: Annotations and Lazy Associations and bad Proxies.
PostPosted: Fri Aug 01, 2008 5:47 am 
Newbie

Joined: Fri Aug 01, 2008 5:38 am
Posts: 2
Location: Brisbane Australia
I am having trouble setting up a very simple example. I end up with unfixable proxy classes. I have enabled Lazy associations, as recommended. I have not found any good, simple examples, so I suspect a magic option is missing.

Given

@Entity
public class Event {
@Id @GeneratedValue
Long id;
@Column
String title;
@OneToMany(mappedBy="event", fetch=FetchType.LAZY)
Set<Activity> activities = new HashSet();
public Event getThis() {return this;}
}

and

@Entity
public class Activity {
@Id @GeneratedValue
long id;
@ManyToOne(fetch=FetchType.LAZY)
Event event;
@Column
String name;
}

Then

Event event2bPre = (Event)session.get(Event.class, 2L);
Activity act4bPre = (Activity)session.get(Activity.class, 4L);
assertEqual("Second Event", act4bPre.event.title);

Works.

But

Activity act4b = (Activity)session.get(Activity.class, 4L);
assertTrue(act4b.event.getClass().getName()
.contains("Event$$EnhancerByCGLIB")); // Ahh!
Event event2aPost = (Event)session.get(Event.class, 2L);
asertTrue(null == act4b.event.title);

Does not work -- act4b.event.title should not be null.

I think that the weird proxy class is to blame. I have sometimes
even got Entity.getThis() != this in other examples.

The definitions for the classes are as recommended by the book,
AFAIK. I suspect that the annotations are not properly
supported/documented, and have weird JPA issues, and so I would
probably be better off sticking to XML. (Have not tried.)

All help appreciated.

(These sort of issues just cannot happen in SimpleORM.)

Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

do you also have getters? I am not sure right now whether act4bPre.event.title would de-proxy your entity. The class name Event$$EnhancerByCGLIB is also expected when cglib is used for byte code enhancement.

You could try to use javassist as byte code provider (in fact that is the default provider in the latest version of Hibernate) or you could use lazy loading via interception.

Last, but not least you could try to de-proxy the instance manually. Have a look at this thread: http://forum.hibernate.org/viewtopic.php?t=962876.

The mapping looks otherwise ok. Is there are particular problem you are trying to solve?

--Hardy


Top
 Profile  
 
 Post subject: Must use getters
PostPosted: Wed Aug 06, 2008 7:08 pm 
Newbie

Joined: Fri Aug 01, 2008 5:38 am
Posts: 2
Location: Brisbane Australia
Thanks for your reply.

I think that the rule with Hibernate is that you Must always use get methods, unlike POJOs. (I usually do not bother with package local fields.) But it is weird that the second get does not resove things.

Also, is this an effect of using Annotations instead of XML?

I did not see this documented in the book.

Anthony


Top
 Profile  
 
 Post subject: Re: Must use getters
PostPosted: Thu Aug 07, 2008 3:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
But it is weird that the second get does not resove things.

I am not quite sure what you refer to here.

Quote:
Also, is this an effect of using Annotations instead of XML?

No, xml vs. annotations is just about how Hibernate gets configured. Under the hood it is always the same core.

--Hardy


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