【问题标题】:get Parent entity based on child根据子实体获取父实体
【发布时间】: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 只会使您的代码崩溃。

标签: hibernate jpa orm hql


【解决方案1】:

请在您的代码中尝试以下查询。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    相关资源
    最近更新 更多