Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 基于子实体获取父实体_Hibernate_Jpa_Orm_Hql - Fatal编程技术网

Hibernate 基于子实体获取父实体

Hibernate 基于子实体获取父实体,hibernate,jpa,orm,hql,Hibernate,Jpa,Orm,Hql,我正在尝试基于子对象获取父实体: 我的父母是: public Class Parent{ ... @ManyToOne private Child child; //getters & setters } 当我使用此方法获取具有特定子女的父母时,如: public ArrayList<Parent>getParentsByChild(Child child){ String req="from Parent p where p.child= :child"; Qu

我正在尝试基于子对象获取父实体: 我的父母是:

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;
}
public ArrayListgetParentsByChild(子-子){
String req=“来自父p,其中p.child=:child”;
Query Query=em.createQuery(req,Parent.class);
query.setParameter(“child”,child);
@抑制警告(“未选中”)
ArrayList父项=(ArrayList)query.getResultList();
返回父母;
}
我得到了无效标识符的例外,问题是我甚至可以得到带有特定子级的父列表,如果是,我必须在查询中更改什么


感谢您的帮助,

请在代码中尝试以下查询

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

请在代码中尝试以下查询

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

尝试添加堆栈跟踪。还要添加父级和子级在数据库级别的关联方式。另外,不要强制转换到ArrayList。如果你得到一个列表,就用这个列表。如果Hibernate决定使用另一个列表实现,则强制转换到ArrayList只会使代码崩溃。请尝试添加stacktrace。还要添加父级和子级在数据库级别的关联方式。另外,不要强制转换到ArrayList。如果你得到一个列表,就用这个列表。如果Hibernate决定使用另一个列表实现,则转换到ArrayList只会使代码崩溃。