如何用jpa规范替换下面的hibernate查询
String hql = "select distinct m " +
"from Membership ms " +
"left join ms.member m " +
"where lower(m.lastName) = :lastName " +
"and lower(m.firstName) = :firstName " +
"and m.gender = :gender ";
这里的主要问题是我希望在member(这是一个left join)对象上有一个不同的记录,而不是membership。下面是我尝试的:
@Override
public Specification<Member> buildSpecification(Membership m) {
Member member = m.getMember();
return (root, query, criteriaBuilder) -> {
PredicateBuilder predicateBuilder = new PredicateBuilder(criteriaBuilder);
Join<?, ?> memberJoin = root.join("member", JoinType.LEFT);
List<Predicate> predicates = predicateBuilder
.addEqualLowerPredicate(member.getLastName().toLowerCase(), memberJoin.get("lastName"))
.addEqualLowerPredicate(member.getFirstName().toLowerCase(), memberJoin.get("firstName"))
.addEqualPredicate(member.getGender(), memberJoin.get("gender"))
.getPredicates();
query.distinct(true);
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}
在这里应用distinct as true后,是给我成员身份distinct object而不是成员身份distinct object
query.distinct(true);
所以在这里我如何得到成员distinct对象,它又是一个左连接。实体结构如下:
@Entity
public class Membership {
private String memberId;
private Date startDate;
private Date endDate;
@ManyToOne
private Member member;
.......
......
}
@Entity
public class Member {
private String firstName;
private String lastName;
private MemberGender gender;
.......
......
}
我对jpa规范非常陌生,非常感谢您的帮助。提前谢谢!!!
暂无答案!
目前还没有任何答案,快来回答吧!