NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JPA: build a query with wildcards using entities

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
Xavier Callejas
Posted via mailing list.





PostPosted: Sat Mar 14, 2009 11:01 pm    Post subject: JPA: build a query with wildcards using entities Reply with quote

Hi,

I need to create a query with wildcard so the user could filter the data.

In a simple SQL query I can use the word 'LIKE' to specify a value for a
paramter in a WHERE clause or if the user did'nt choose any filter I pass
'%' to the LIKE clause and I get all the rows.

But using JPQL I cannot pass a '%' to a parameter especting a entity object,
nither use the LIKE clause for a paramter especting a entity object.

In simply SQL, the query I would like is like this:

SELECT * FROM Clients WHERE Clients.Country LIKE ( IF(? == NULL, '%', ?) );

That way I have not to build "dinamically" the query (a lot of work).

How can I do something similar in JPQL (Country is a entity object in JPQL,
so I cannot use LIKE on it)?

I imagin to use WHERE Clients.Country.Name, because Name is a String not a
entity, so I could use LIKE, but is this way recommended?

Thank you in advance.
Back to top
Unmesh Chowdhury



Joined: 09 Dec 2008
Posts: 6

PostPosted: Sun Mar 15, 2009 10:10 am    Post subject: Reply with quote

Hello,
You would try something like this –

“select c from Clients c where c.country.name like :name”

Where,
Clients and Country are entity classes and there is a Many-to-One relationship between Clients and Country. In the Clients instance country represents the Country instance with which the present client is related to. And name is an attribute of Country class.

Now you can pass the parameter (name) value with wildcard.

Thanks
u.c.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo