如何在HQL中获取多个到一个映射数据

时间:2015-07-19 12:24:11

标签: java hibernate hql

我在Java中有两个对象

@Entity
public class Car {
    @Id
    private long id;
    private int color;
    @ManyToOne
    private Admin admin; 
    //setter-getter   
}


@Entity
Class Admin{
    @Id
    private int id;
    private String name;
    //setter-getter
}

和获取

Admin admin=(Admin) session.getAdmin("user");
....
tx = session.getTransaction();
tx.begin();
list = session.createQuery("from Car where admin="+admin.getId()).list();
tx.commit();
....

但我收到了错误。任何人都可以告诉HQL查询是什么。

1 个答案:

答案 0 :(得分:1)

试试这个:

list = session.createQuery("from Car c where c.admin = "+admin.getId()).list();