【发布时间】:2017-09-27 18:19:01
【问题描述】:
我正在尝试根据子对象获取父实体: 我有像这样的实体父:
public Class Parent{
...
@ManyToOne
private Child child;
//getters & setters
}
当我使用这种方法来获取具有特定孩子的父母时:
public ArrayList<Parent>getParentsByChild(Child child){
String req="from Parent p where p.child= :child";
Query query = em.createQuery(req, Parent.class);
query.setParameter("child", child);
@SuppressWarnings("unchecked")
ArrayList<Parent> parents= (ArrayList<Parent>) query.getResultList();
return parents;
}
我得到了无效标识符的异常,问题是我什至可以得到一个特定孩子的父母列表,如果是,我必须在查询中更改什么。
感谢您的帮助,
【问题讨论】:
-
尝试添加堆栈跟踪。还要添加数据库级别的父子关系。
-
另外,不要强制转换为 ArrayList。你得到一个列表,使用那个列表。如果 Hibernate 决定使用另一个 List 实现,强制转换为 ArrayList 只会使您的代码崩溃。