gpt4 book ai didi

java - LIKE - hibernate 命名查询

转载 作者:行者123 更新时间:2023-11-29 04:42:04 27 4
gpt4 key购买 nike

为什么我在使用这个命名查询时得到语法错误:

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

和:

Query query = session.getNamedQuery("event_find").setParameter("address", address); // or "%"+address+"%"

我该如何解决这个问题?

编辑:

事件:

@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;
}

...

}

地点:

@Entity
@Table(catalog = "control_station")
public final class Location implements Serializable {

private long id;
private String address;
private double latitude;
private double longitude;

...
}

例如假设我们在表 Location 中有三个地址:

  1. 西类牙
  2. 西类牙马德里
  3. 圣地亚哥伯纳乌球场,马德里,西类牙

现在如果我搜索Madrid, Spain,结果必须包含以上所有...

即:

哪里西类牙喜欢%Madrid, Spain%或者Madrid, Spain喜欢%Spain%

Santiago Bernabeu Stadium, Madrid, Spain%Madrid, Spain%Madrid, Spain%Santiago Bernabeu Stadium , 马德里, 西类牙%

等等……

最佳答案

谢谢大家,答案是:

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

关于java - LIKE - hibernate 命名查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com