• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Using Named Queries

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yall,
I am starting to use named queries, and am not sure how to handle one thing. I have a query like "from myTable". If the user passes in a firstName, or lastName, or Date of birth, the query should be however, at runtime, I am not sure if the user is going to pass any or all of the parameters.

I want to have this query defined in my file NamedQuery.hbm.xml



I just don't know how to handle deciding on the fly if I need to remove one of the things like lname = :lastName from my query.

Can I do something like this in the code (use a wildcard)


Thanks if anyone has an idea.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kim Kantola,

I've never used named queries myself but my first guess is if you want to use a wildcard you should probably try the SQL-wildcard of your DBMS. That is, most likely, the per-cent sign "%".

CU
Froestel

 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, yes, using the "%" did the trick.

It however brought me to a new problem, one of the fields I am trying to compare is a date, so if I have this in my query:


as part of my query, and my DOB value is null so I do this :


I then get the follwing error :

java.lang.ClassCastException
at org.hibernate.type.DateType.toString(DateType.java:78)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:89)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:63)
at org.hibernate.loader.hql.QueryLoader.bindNamedParameters(QueryLoader.java:491)


Is there a special wildcard to use for dates ?

Thanks!
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually for queries which are dynamic like that, it is easier to use the Criteria object instead.

then you can have code like

from myTable where fname= :firstName AND lname= :lastName and DOB =OB



Mark
 
Slime does not pay. Always keep your tiny ad dry.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic