什么相当于使用实体管理器的hibernate 5+中的.createAlias

时间:2017-02-26 16:46:49

标签: hibernate jpa annotations

我有两个实体人员和帐户,一对一的关系。 在我的旧休眠中,我有以下方法来过滤人。

public List filterByPersonalInfo(String personal_info) throws Exception{
    List values = null;
    Session session = getSession();
    Criteria criteria = getHBCriteria(session, personal_info);

    criteria.addOrder(Order.desc("person_id"));
    values = criteria.list();
    return values;
}

private Criteria getHBCriteria(Session session, String personal_info) throws       Exception {

Criteria criteria = session.createCriteria(Person.class).createAlias("account", "acc")
            .add(
                    Restrictions.disjunction()

                    .add(Restrictions.ilike("first_name", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("middle_name", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("last_name", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("cnic", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("gender", personal_info, MatchMode.START))
                    .add(Restrictions.ilike("zipcode", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("address1", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("address2", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("phone_no", personal_info, MatchMode.ANYWHERE))

                    .add(Restrictions.ilike("acc.username", personal_info, MatchMode.ANYWHERE))
                    .add(Restrictions.ilike("acc.email", personal_info, MatchMode.ANYWHERE))
           );

    return criteria;
}

现在我想切换到Hibernate 5或更高版本(使用jpa注释)。我需要使用实体管理器等同于上面的内容。 到目前为止我所做的如下。

public List<Person> filterByPersonalInfo(String personal_info) throws Exception{
    List<Person> list = null;

      EntityManager em = HibernateUtil.getEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Person> cq = cb.createQuery(Person.class);
      Root<Person> root = cq.from(Person.class);
      EntityType<Person> type = em.getMetamodel().entity(Person.class);

    // Where
      cq.where(
            cb.or(
            cb.like(root.get(type.getDeclaredSingularAttribute("first_name", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("middle_name", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("last_name", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("cnic", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("gender", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("zipcode", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("address1", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("address2", String.class)), "%" + personal_info + "%"),
            cb.like(root.get(type.getDeclaredSingularAttribute("phone_no", String.class)), "%" + personal_info + "%")
            )
    );

    // Order By
    cq.orderBy(cb.desc(root.get("person_id")));

    list = em.createQuery(cq).getResultList();
    return list;
}

我不知道如何过滤帐户的属性,即用户名和电子邮件。

感谢。

0 个答案:

没有答案