-->
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.  [ 6 posts ] 
Author Message
 Post subject: java1.5 reflection to invoke an assoc method of a hib object
PostPosted: Fri Mar 30, 2007 11:28 am 
Newbie

Joined: Wed Dec 20, 2006 11:47 am
Posts: 6
Is there a way to use java 1.5 reflection to invoke an assocation method of an hibernate object? Let's say I have a hbm.xml -

< class="genClass" table="genTable">
...
<set name="classObjects"
...
</set>
</class>

and in my calling routine -

obj = session.load( "genClass", ID);
...
method = myClass.getDeclaredMethod("getClassObjects", null )
method.invoke( obj, null );

This gives me an error -

failed to lazily initialize a collection of role
no session or session was closed
Exception Class: org.hibernate.LazyInitializationException


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 12:57 pm 
Newbie

Joined: Fri Mar 30, 2007 8:19 am
Posts: 6
It doesn't seem a reflection problem but a mapping one. You should set lazy="false" for your collection in your mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 1:26 pm 
Newbie

Joined: Wed Dec 20, 2006 11:47 am
Posts: 6
Yes, you are right. It is a mapping problem. What if I don't want to set fetching to "non-lazy" in my mapping? How can I do it programmatically? I've tried reattaching the object to the session, but it didn't work.

session.refresh(obj)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 2:09 pm 
Newbie

Joined: Fri Mar 30, 2007 8:19 am
Posts: 6
First thing I would do is setting it to non lazy to check if it behaives as you expect, so you can be sure the problem isn't anywere else. Once it's working, you can "lazy it back", and try to find a programmatic workaround.

Maybe you could fill your collection (Set) via hql just after the session.load() call and the set it to your object with myClass.setClassObects(...). This is something that I've done several times when I didn't want to set a non lazy relation.

Hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 2:28 pm 
Newbie

Joined: Wed Dec 20, 2006 11:47 am
Posts: 6
The whole point of having a <set> inside the mapping is so that one can easily retrieve the set of associated objects with a

Object obj = session.load( genClass, ID);
Set set = obj.getClassObjects()

Why do I have to write hsq for the objects and setting it to obj when I should just be able to use "getClassObjects()"?

I am rather hesitant in setting the mapping to "non-lazy" because of performance issues. What if I just want to load genClass, but don't really need the objects?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 2:43 pm 
Newbie

Joined: Fri Mar 30, 2007 8:19 am
Posts: 6
That's exactly what you have with your lazy initialization, and tha's why you are getting that exception, because you're trying to access an object that was never created. Neither you or hibernate did it at any moment.

Maybe nonsense, but, what if you try something like:

myclass.classObjects = session.load(Set.class, [Set id here]);

Maybe the set mapping makes that work, but I don't know how are you going to get the Set id.

Just giving ideas.

However, I'm really interested in how this topic is solved, because, as you pointed before, the way I understand the lazy inicialization makes you loose the ORM capability.


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