简体   繁体   中英

LIKE - hibernate named queries

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 :

  1. Spain
  2. Madrid, Spain
  3. Santiago Bernabeu Stadium, Madrid, Spain

now if I search for Madrid, Spain , the result must contains all of the above...

ie :

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...

谢谢你们,答案是:

@NamedQuery(name = "event_find", query = "from Event where location.address like concat('%', :address, '%') or :address like concat('%', location.address, '%')")

I am not an expert with Hibernate but for what I know you probably have two problems :

  • First I think you need to put the % in the setParameter() call, not directly in the named query. This also means you'll end up with two parameters.
  • Second, I assume 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); 

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 "%"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM