HibernateCallback与Java 8 lambda代码

时间:2018-04-25 08:07:09

标签: java hibernate lambda java-8

如何使用Java 8 lambda正确地重构此示例?

我正在尝试在以下示例中使用Java 8 lambda:

hibernateTemplate.execute(new HibernateCallback<HumanMainInfo>() {
    @Override
    public HumanMainInfo doInHibernate(Session session) throws HibernateException {
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<HumanMainInfo> query = builder.createQuery(HumanMainInfo.class);
        Root<SysUserMainInfo> sysUserRoot = query.from(SysUserMainInfo.class);
        Join<SysUserMainInfo, HumanMainInfo> humanJoin = sysUserRoot.join("human", JoinType.INNER);
        Predicate idCondition = builder.equal(sysUserRoot.get("id"), userId); 
        query.where(idCondition).select(humanJoin);
        return session.createQuery(query).uniqueResult();
    }
});

1 个答案:

答案 0 :(得分:1)

hibernateTemplate.execute(session -> {
     CriteriaBuilder builder = session.getCriteriaBuilder();
     CriteriaQuery<HumanMainInfo> query = builder.createQuery(HumanMainInfo.class);
     Root<SysUserMainInfo> sysUserRoot = query.from(SysUserMainInfo.class);
     Join<SysUserMainInfo, HumanMainInfo> humanJoin = sysUserRoot.join("human", JoinType.INNER);
     Predicate idCondition = builder.equal(sysUserRoot.get("id"), userId); 
     query.where(idCondition).select(humanJoin);
     return session.createQuery(query).uniqueResult();
});