hibernate:在嵌入对象上运行查询/分离标准时出现问题

时间:2010-11-27 13:08:02

标签: java sql hibernate detachedcriteria

我遇到了在嵌入对象上运行查询或分离条件的问题。我试过从this related question接近,但似乎都不起作用。

我得到的错误是:

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();

1 个答案:

答案 0 :(得分:1)

HQL中的属性区分大小写,您需要location.longitude