根据孩子获得父母实体



我正在尝试基于子对象获得父实体:我有实体父母,例如:

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;
}

我得到了无效的标识符,问题是我什至可以和一个特定的孩子一起获得父母列表,如果是,我必须在查询中更改。

感谢您的帮助,

请尝试在代码中的查询之后。

String hql = "select p from Parent p where p.child = :child" 

最新更新