Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Query by example in eclipselink(Query by example in eclipselink)
Query by example in eclipselink [message #1425011] Tue, 16 September 2014 18:52 Go to next message
Nicolas Steinhilber is currently offline Nicolas SteinhilberFriend
Messages: 2
Registered: September 2014
Junior Member
Hi! I am newbie in eclipselink and i don't know how to do a query by example like in hibernate.

In hibernate I have

Criteria crit = session.createCriteria(type);
Example example = Example.create(obj);
example.excludeZeroes();
example.enableLike(MatchMode.ANYWHERE).ignoreCase();
crit.add(example);

Then... I want to do a query where always the query has a like %parameter% in every string property.

If a do...
ReadAllQuery q = new ReadAllQuery();
q.setExampleObject(obj);
QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "like");
q.setQueryByExamplePolicy(policy);

The comodin % must be put by me in every field... and I want to avoid this situation like I did in hibernate....

It is possible?
How?
Can you help me?

Thanks in advance and excuse me but my english is pretty poor....

Nicolas
Re: Query by example in eclipselink [message #1429159 is a reply to message #1425011] Mon, 22 September 2014 22:29 Go to previous messageGo to next message
Ralf Heydenreich is currently offline Ralf HeydenreichFriend
Messages: 235
Registered: July 2009
Senior Member
Am 16.09.2014 um 22:59 schrieb Nicolas Steinhilber:
> Hi! I am newbie in eclipselink and i don't know how to do a query by
> example like in hibernate.
>
> In hibernate I have
>
> Criteria crit = session.createCriteria(type);
> Example example = Example.create(obj);
> example.excludeZeroes();
> example.enableLike(MatchMode.ANYWHERE).ignoreCase();
> crit.add(example);
>
> Then... I want to do a query where always the query has a like
> %parameter% in every string property.
>
> If a do...
> ReadAllQuery q = new ReadAllQuery();
> q.setExampleObject(obj);
> QueryByExamplePolicy policy = new QueryByExamplePolicy();
> policy.addSpecialOperation(String.class, "like");
> q.setQueryByExamplePolicy(policy);
>
> The comodin % must be put by me in every field... and I want to avoid
> this situation like I did in hibernate....
>
> It is possible?
> How?
> Can you help me?
>
> Thanks in advance and excuse me but my english is pretty poor....
>
> Nicolas

Perhaps you can use this:

org.eclipse.persistence.expressions.Expression.containsSubstring(String
theValue)

PUBLIC: Return an expression that compares if the receivers value
contains the substring.

Example:

EclipseLink: employee.get("firstName").containsSubstring("Bob")
Java: employee.getFirstName().indexOf("Bob") != -1
SQL: F_NAME LIKE '%BOB%'




I've a similar situation. But I don't know how to execute my
ReadAllQuery... How do you do this? EntityManager doesn't have an
execute() method for such queries...

Ralf.
Re: Query by example in eclipselink [Update] [message #1429162 is a reply to message #1429159] Mon, 22 September 2014 22:34 Go to previous messageGo to next message
Ralf Heydenreich is currently offline Ralf HeydenreichFriend
Messages: 235
Registered: July 2009
Senior Member
Am 23.09.2014 um 00:29 schrieb Ralf Heydenreich:
> Am 16.09.2014 um 22:59 schrieb Nicolas Steinhilber:
>> Hi! I am newbie in eclipselink and i don't know how to do a query by
>> example like in hibernate.
>>
>> In hibernate I have
>>
>> Criteria crit = session.createCriteria(type);
>> Example example = Example.create(obj);
>> example.excludeZeroes();
>> example.enableLike(MatchMode.ANYWHERE).ignoreCase();
>> crit.add(example);
>>
>> Then... I want to do a query where always the query has a like
>> %parameter% in every string property.
>>
>> If a do...
>> ReadAllQuery q = new ReadAllQuery();
>> q.setExampleObject(obj);
>> QueryByExamplePolicy policy = new QueryByExamplePolicy();
>> policy.addSpecialOperation(String.class, "like");
>> q.setQueryByExamplePolicy(policy);
>>
>> The comodin % must be put by me in every field... and I want to avoid
>> this situation like I did in hibernate....
>>
>> It is possible?
>> How?
>> Can you help me?
>>
>> Thanks in advance and excuse me but my english is pretty poor....
>>
>> Nicolas
>
> Perhaps you can use this:
>
> org.eclipse.persistence.expressions.Expression.containsSubstring(String
> theValue)
>
> PUBLIC: Return an expression that compares if the receivers value
> contains the substring.
>
> Example:
>
> EclipseLink: employee.get("firstName").containsSubstring("Bob")
> Java: employee.getFirstName().indexOf("Bob") != -1
> SQL: F_NAME LIKE '%BOB%'
>
>
>
>
> I've a similar situation. But I don't know how to execute my
> ReadAllQuery... How do you do this? EntityManager doesn't have an
> execute() method for such queries...
>
> Ralf.
>


To be a little more concrete:

ReadAllQuery q = new ReadAllQuery();
q.setExampleObject(obj);
QueryByExamplePolicy policy = new QueryByExamplePolicy();
policy.addSpecialOperation(String.class, "containsSubstring");
q.setQueryByExamplePolicy(policy);


HTH,
Ralf.
Re: Query by example in eclipselink [Update] [message #1429514 is a reply to message #1429162] Tue, 23 September 2014 10:30 Go to previous message
Nicolas Steinhilber is currently offline Nicolas SteinhilberFriend
Messages: 2
Registered: September 2014
Junior Member
Hi Ralf!
Thanks for your answer... but I want to do in a generic way... Where my method works most of the time, eventually I will have to override it....
about your question... try this
Query query = JpaHelper.createQuery(q, entityManager);
 return query.getResultList();


Hope that helps

Nicolás

[Updated on: Tue, 23 September 2014 11:01]

Report message to a moderator

Previous Topic:Multiple JTA data sources for single PU
Next Topic:Managing Multiple Database Connections
Goto Forum:
  


Current Time: Fri Apr 26 11:23:35 GMT 2024

Powered by FUDForum. Page generated in 0.02390 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top