Reputation: 1359
why I get syntax error using this named query :
@NamedQuery(name = "event_find", query = "from Event where location.address like str('%'+:address+'%') " +
"or :address like str('%'+location.address+'%'")
and :
Query query = session.getNamedQuery("event_find").setParameter("address", address); // or "%"+address+"%"
how can I solve this problem?
EDIT :
Event :
@NamedQueries({
@NamedQuery(name = "event_find", query = "from Event where location.address like :address " +
"or :address like location.address")
})
@Entity
@Table(catalog = "control_station")
public final class Event implements Serializable {
private long id;
private Location location;
...
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
...
}
Location :
@Entity
@Table(catalog = "control_station")
public final class Location implements Serializable {
private long id;
private String address;
private double latitude;
private double longitude;
...
}
for example suppose we have three addresses in table Location
:
now if I search for Madrid, Spain, the result must contains all of the above...
i.e :
where Spain like %Madrid, Spain% or Madrid, Spain like %Spain%
where Santiago Bernabeu Stadium, Madrid, Spain like %Madrid, Spain% or Madrid, Spain like %Santiago Bernabeu Stadium, Madrid, Spain%
and so on...
Upvotes: 1
Views: 752
Reputation: 1359
thank you guys, the answer is :
@NamedQuery(name = "event_find", query = "from Event where location.address like concat('%', :address, '%') or :address like concat('%', location.address, '%')")
Upvotes: 2
Reputation: 21425
The named query should be like this:
@NamedQuery(name = "event_findLike", query = "from Event where location.address like str(:address) " +
"or :address like :locationAddress
you should not use %
in this named query.
You can use the %
symbol while setting the parameters like this:
Query query = session.getNamedQuery("event_findLike")
.setParameter("address", '%'+address+'%')
.setParameter("locationAddress", '%'+locationAddress+'%');
In this code replace address
and locationAddress
with your variables that contain required information.
Also refer to this link for similar information: How to correctly convert JPQL query using "%"
Upvotes: 1
Reputation: 3618
I am not an expert with Hibernate but for what I know you probably have two problems :
%
in the setParameter()
call, not directly in the named query. This also means you'll end up with two parameters.str()
is not needed since location.address
is probably already a string. Moreover, it looks like there is a missing parenthesis at the end of your str()
call.So, I would try something like that :
@NamedQuery(name = "event_findLike", query = "from Event where location.address like :addressLike " +
"or :address like '%'+location.address+'%'")
and
Query query = session.getNamedQuery("event_findLike").setParameter("addressLike", "%"+address+"%").setParameter("address", address);
Upvotes: 1