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

发布于 2024-10-04 21:25:08 字数 2906 浏览 4 评论 0原文

我在嵌入对象上运行查询或分离条件时遇到问题。我已经尝试过这个相关问题中的两种方法,似乎都不起作用。

我收到的错误是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-10-11 21:25:08

HQL 中的属性区分大小写,您需要使用 location.longitude 来代替。

Properties in HQL are case sensitive, you need location.longitude instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文