Using wildcard % in Hibernate (v 3.2.5) with named query

T

traneHead

Trying to use a named query put in a hbm like:

<query name="Point.findByTitle">
from Point point where point.title like :title
<query-param name="title" type="String" />
</query>

except I'd like to put wildcards on the named parameter like '%:title
%' but then I get a IAE: "No positional parameters in query"
so I'm guessing that the :title parameter isn't discovered by
hibernate then. I've tried different methods of escaping the % and
other esoteric formatting but no-go...

Any suggestions on how to do this?
 
L

Lew

traneHead said:
Trying to use a named query put in a hbm like:

<query name="Point.findByTitle">
from Point point where point.title like :title
<query-param name="title" type="String" />
</query>

What advantage does aliasing "Point" to "point" confer?
 
L

Lew

traneHead said:
None in this example.
Why? Does it have anything to do with the wildcard question?

Because it caught my eye, and I was curious. Usually an alias table name is a
shorter version, so the duplication stood out for me.

This is a discussion group. I was discussing.
 
T

traneHead

Because it caught my eye, and I was curious. Usually an alias table name is a
shorter version, so the duplication stood out for me.

This is a discussion group. I was discussing.

OK, no the example query might as well (and should) be written 'from
Point where title like :title', the aliasing was a leftover from
copying/pasting/deleting (although not enough of the latter
apparently ;))
 
L

Lew

traneHead said:
Trying to use a named query put in a hbm like:
<query name="Point.findByTitle">
from Point point where point.title like :title
<query-param name="title" type="String" />
</query>

I reviewed the Java Persistence API today. Looks to me like :varName is a
syntax for a variable only; there is no concatenation to surrounding tokens.
You will have to include the wildcard ('%') in the substitution value for the
named parameter.
 
T

traneHead

I reviewed the Java Persistence API today. Looks to me like :varName is a
syntax for a variable only; there is no concatenation to surrounding tokens.
You will have to include the wildcard ('%') in the substitution value for the
named parameter.

Thank you. That's the way.
 
A

Adam Maass

traneHead said:
Trying to use a named query put in a hbm like:

<query name="Point.findByTitle">
from Point point where point.title like :title
<query-param name="title" type="String" />
</query>

except I'd like to put wildcards on the named parameter like '%:title
%' but then I get a IAE: "No positional parameters in query"
so I'm guessing that the :title parameter isn't discovered by
hibernate then. I've tried different methods of escaping the % and
other esoteric formatting but no-go...

try:

from Point where title like concat ('%', :title, '%')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top