-->
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: How to create a new Criteria class based on another Criteri
PostPosted: Tue Nov 20, 2007 1:17 pm 
Newbie

Joined: Tue Nov 20, 2007 11:03 am
Posts: 4
Location: Brazil
Buddy's,

I want to create one Criteria class based on another that have just his filters. But I want a new class, not a reference to the original class. Basically I new instance, a clone,

Does anybody know how do I do it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 8:57 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Cloning comes in JAVA....

Anyway..Use the Cloneable interface to have the clone of the other object. To have a deep clone write it into a file and then read it again.
or Use BeanUtils class to do the cloning.

You can search the Cloneable interface in google for examples.

Hope this helps.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 8:39 am 
Newbie

Joined: Tue Nov 20, 2007 11:03 am
Posts: 4
Location: Brazil
Unfortunately the beanutils can't clone the Criteria object. And To create a new Criteria that implements Cloneable is difficult, cause we're talking about a framework class and where we access acrossing an interface.

Anyway thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 5:44 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Did u try the deep clone????

i.e)
try {
Criteria criteria = session.createCriteria(CategoryInfo.class);
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(b);
out.writeObject(criteria);
ByteArrayInputStream bIn = new ByteArrayInputStream(b.toByteArray());
ObjectInputStream oi = new java.io.ObjectInputStream(bIn);
Criteria clonedCriteria = (Criteria) oi.readObject();
}catch (Exception e) {
e.printStackTrace();
}

Hope this helps:-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 10:39 am 
Newbie

Joined: Tue Nov 20, 2007 11:03 am
Posts: 4
Location: Brazil
Oh, Sorry. I forgot trying this.

I tryied rigth now. But there is a trouble. When I call the "list()" method, it throws a NullPointerException. I had a look at the source code of hibernate and I've see that the error is that it looses the reference to the session.

I solved doing this:

( (CriteriaImpl) clonedCriteria).setSession( (SessionImpl)getSession() );

I had to expose the implementation, because the setSession existis only on the CriteriaImpl and signature of the method get SessionImpl.

Is there another way? Or necessarily I need to expose the implementation?

Is there some trouble exposing the implementation?

Really thank you anyway. You're just helping me very much


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 2:10 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
I don't know any other way:( May be others can help:-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


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.