如何在JPA Criteria Queries中动态创建Clause的位置,

时间:2016-02-10 14:18:24

标签: java mysql hibernate jpa

这是我的Jpa查询标准......

EntityManagerFactory em;
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SearchUser> cq = cb.createQuery(SearchUser.class);
    Root<SearchUser> userRoor = cq.from(SearchUser.class);
    cq.select(userRoot);
    TypedQuery<SearchUser> query = ((EntityManager) em).createQuery(cq);
    List<SearchUser> users = query.getResultList();

这是我正在动态创建的Where子句...现在我希望jpa根据这个动态生成的where子句搜索结果结果...但是不知道如何用select查询附加这个where子句...任何人都可以编辑并向我显示正确的查询请...提前致谢

StringBuilder whereClause = new StringBuilder();
    whereClause.append("where ")
    if (searchUserDTO.getId() != null) {
        whereClause.append("id=" + searchUserDTO.getId());
        }
        if (searchUserDTO.getFirstName() != null) {
            whereClause.append(" and firstName=" + searchUserDTO.getFirstName());
        }
        if (searchUserDTO.getLastName() != null) {
            whereClause.append(" and lastName=" + searchUserDTO.getLastName());
        }
        if (searchUserDTO.getDateOfBirth() != null) {
            whereClause.append(" and dateOfBirth=" + searchUserDTO.getDateOfBirth());
            }
        if (searchUserDTO.getEmail() != null) {
            whereClause.append(" and email=" + searchUserDTO.getEmail());
        }

0 个答案:

没有答案