Question

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();
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top