-->
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.  [ 9 posts ] 
Author Message
 Post subject: find by example
PostPosted: Wed Jun 20, 2007 2:45 pm 
Newbie

Joined: Tue Jan 23, 2007 3:25 am
Posts: 5
Hi everyone,
I need some help doing a query...
Let's assume:

Code:
class A{
String attributeA1;
String attributeA2;

}

class B{
String attributeB;
A a;
}


......

Code:
B exampleB = new B();
A exampleA = new A();
exampleA.attributeA1 = "attributeA1";
exampleA.attributeA2 = "attributeA2";
exampleB .setA( exampleA  );

......

I am trying to run the following query:

Code:
Example example = Example.create( B );
example.enableLike(MatchMode.ANYWHERE);

Criteria criteria = session.createCriteria( B.getClass() );
criteria.add( example );

List l =  criteria.add(example).list();


It doesn't work...search ignores the attributes set in A. It works only for the attribute inside class B.
I'm doing something wrong????
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 12:16 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Can you send more detail about

Example example = Example.create( B );
example.enableLike(MatchMode.ANYWHERE);


and

What you want in words.


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:11 am 
Newbie

Joined: Tue Jan 23, 2007 3:25 am
Posts: 5
amila733 wrote:
Hi

Can you send more detail about

Example example = Example.create( B );
example.enableLike(MatchMode.ANYWHERE);


and

What you want in words.


Amila


Thanks for your answer....(..and sorry for my bad english)

In words...i need to to a search by example using the fields contained in a class A but using a class B which has a reference to A.
For example...B is an object representing a client which has a reference to an object A which contains all the info about B Employer.
I want to search all the Clients that have an Employer with a particular attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:19 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

what is Example

and what is the implementatiopn of Example.create( B );


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 4:03 am 
Newbie

Joined: Tue Jan 23, 2007 3:25 am
Posts: 5
amila733 wrote:
Hi

what is Example

and what is the implementatiopn of Example.create( B );


Amila



org.hibernate.criterion.Example

Looking at chapter 12.6. Example queries
[url]
http://www.hibernate.org/hib_docs/refer ... teria.html
[/url]

Quote:
The class net.sf.hibernate.expression.Example allows you to construct a query criterion from a given instance.

Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.

You can adjust how the Example is applied.



maybe I found the solution...in theese lines of manuals...associations are ignored

Is there a way to do an example query using associations too??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 5:16 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Sorry i thought it is your own class


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 5:51 am 
Newbie

Joined: Tue Jan 23, 2007 3:25 am
Posts: 5
amila733 wrote:
Hi

Sorry i thought it is your own class


Amila


Don't worry....thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:37 pm 
Newbie

Joined: Mon Mar 05, 2007 9:04 am
Posts: 6
Location: Belarus
I think this should work:
Code:
List l = session.createCriteria(B.getClass())
         .add(Example.create(exampleB))
         .createCriteria("a")
         .add(Example.create(exampleB.getA()))
         .list();


But i can't figure out what should I do to make it working for the case when I need to supply examples for more than one nested objects (.createCriteria("a") roots criterion in the "a" property so all subsequent .createCriterias are applied to the a property)

Someone knows?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 3:42 pm 
Newbie

Joined: Tue Jan 23, 2007 3:25 am
Posts: 5
Yes should work...however that was just an example, i've got many associations and settings them manually (for each kind of root object) is impossible...

However i found the solution in this post:
[url]
http://forum.hibernate.org/viewtopic.php?t=942872
[/url]

..it works really well!!


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