hibernate:在嵌入式对象上运行查询/分离标准时出现问题
我在嵌入对象上运行查询或分离条件时遇到问题。我已经尝试过这个相关问题中的两种方法,似乎都不起作用。
我收到的错误是:
org.hibernate.QueryException: could not resolve property: location.Longitude of: org.project.model.Event [select c from org.project.model.Event as c WHERE c.location.Longitude between :minLongitude AND :maxLongitude]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:82)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
...
是我的代码:
@Entity
@Table(name = "Events")
public class Event extends Identifiable {
private Location location;
private DateTime createdTS;
@Embedded
public Location getLocation() {
return location;
}
public Event setLocation(Location location) {
this.location = location;
return this;
}
@Column(name = "Created")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
public DateTime getCreatedTS() {
return createdTS;
}
public Event setCreatedTS(DateTime createdTS) {
this.createdTS = createdTS;
return this;
}
}
这
@Embeddable
public class Location {
private double longitude;
private double latitude;
public Location(double longitude, double latitude) {
setLongitude(longitude);
setLatitude(latitude);
}
public Location() {}
@Column(name = "Longitude")
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Column(name = "Latitude")
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
}
我尝试使用查询:
Query q = session.createQuery("select c from Event as c WHERE c.location.Longitude between :minLongitude AND :maxLongitude");
q.setParameter("minLongitude", minLongitude);
q.setParameter("maxLongitude", maxLongitude);
return (List<Event>)q.list();
并使用 DetachedCriteria:
final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Event.class);
detachedCriteria.add(Restrictions.between("Location.Longitude", minLongitude, maxLongitude));
detachedCriteria.add(Restrictions.between("Location.Latitude", minLatitude, maxLatitude));
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return (List<Event>) criteria.list();
I'm encountering a problem running a query or detached criteria on an embedded object. I've tried both approached from this related question, neither seem to work.
The error I'm getting is:
org.hibernate.QueryException: could not resolve property: location.Longitude of: org.project.model.Event [select c from org.project.model.Event as c WHERE c.location.Longitude between :minLongitude AND :maxLongitude]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:82)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
...
Here is my code:
@Entity
@Table(name = "Events")
public class Event extends Identifiable {
private Location location;
private DateTime createdTS;
@Embedded
public Location getLocation() {
return location;
}
public Event setLocation(Location location) {
this.location = location;
return this;
}
@Column(name = "Created")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
public DateTime getCreatedTS() {
return createdTS;
}
public Event setCreatedTS(DateTime createdTS) {
this.createdTS = createdTS;
return this;
}
}
and this
@Embeddable
public class Location {
private double longitude;
private double latitude;
public Location(double longitude, double latitude) {
setLongitude(longitude);
setLatitude(latitude);
}
public Location() {}
@Column(name = "Longitude")
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Column(name = "Latitude")
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
}
I've tried using a Query:
Query q = session.createQuery("select c from Event as c WHERE c.location.Longitude between :minLongitude AND :maxLongitude");
q.setParameter("minLongitude", minLongitude);
q.setParameter("maxLongitude", maxLongitude);
return (List<Event>)q.list();
And using a DetachedCriteria:
final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Event.class);
detachedCriteria.add(Restrictions.between("Location.Longitude", minLongitude, maxLongitude));
detachedCriteria.add(Restrictions.between("Location.Latitude", minLatitude, maxLatitude));
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
return (List<Event>) criteria.list();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HQL 中的属性区分大小写,您需要使用
location.longitude
来代替。Properties in HQL are case sensitive, you need
location.longitude
instead.