hibernate查询按其他表的条件查找

时间:2017-02-23 14:25:25

标签: java hibernate

这是原始查询:

public List<Ride> listRides(Long userId) {
        List<Ride> ride = Collections.EMPTY_LIST;
            ride = getHibernateTemplate()
                    .find("from Ride v where v.userId = ? AND finished = true AND isDeleted = false order by startTime DESC",
                            userId);

        return ride.size() > 0 ? ride : Collections.EMPTY_LIST;
    }

现在我需要检查是否允许显示行车ID,为此我需要检查另一张表。 表结构是:

乘坐表:

id bigint NOT NULL DEFAULT nextval('hd_routes_id_seq'::regclass),
ride_name character varying(40),
category_id bigint,
etc....

RidePrivacy表:

id bigint NOT NULL DEFAULT nextval('hd_rides_privacy_id_seq'::regclass),
ride_id bigint,
is_visible boolean

我把它添加到Ride类:

@OneToOne(cascade = CascadeType.ALL)
    @JoinTable(name = "RidesPrivacy", joinColumns = @JoinColumn(name = "id") , inverseJoinColumns = @JoinColumn(name = "ride_id") )

并重做dao实现:

public List<Ride> listRides(Long userId) {
        List<Ride> ride = Collections.EMPTY_LIST;
            StringBuilder query = new StringBuilder();
            query.append("from Ride v where v.userId = ? AND finished = true");
            query.append("AND Ride.RidesPrivacy.isVisible= true ");

            query.append("AND isDeleted = false order by startTime DESC");

            ride = getHibernateTemplate().find(query.toString(), userId);


        return ride.size() > 0 ? ride : Collections.EMPTY_LIST;
    }

但是当我执行它时它会返回我:

org.springframework.orm.hibernate3.HibernateQueryException: Invalid path: 'null.RidesPrivacy.isVisible'

0 个答案:

没有答案