查询嵌入式主键列表

时间:2017-10-17 04:28:27

标签: java mysql hibernate

我尝试使用嵌入式主键列表查询对象列表。像这样:

User.java

public class User {
   @EmbeddedId
   protected UserPK userPK;

   //getter & setters...
}

UserPK.java

@Embeddable
public class UserPK {
    @Column(name = "id")
    private Long id;

    @Column(name = "group")
    private String group;

    //getter & setters...
}

UserDao.java

public List<Long> getUsersById(List<UserPK> ids) {
    TypedQuery<User> q = entityManager.createQuery(
        "SELECT u FROM User AS u WHERE u.userPK IN :ids", User.class);
    q.setParameter("ids", ids);
    return q.getResultList();
}

基本上,我要查询多个用户,让我们说:

id = 1 group =&#34; it&#34;
id = 2 group =&#34; eng&#34;

如何使用复合主键列表查询用户?

1 个答案:

答案 0 :(得分:1)

你所做的一切都很好,虽然getUsersById方法的返回类型需要进行小的修正,而List<User>代替List<Long>

您可以使用UserPKs对象列表进行查询,并将其用作IN运算符的参数。所以下面的查询(就像你一样)将正常工作:

public List<User> getUsersByIdCombo(List<UserPK> ids) {
    TypedQuery<User> q = entityManager.createQuery("SELECT u FROM User AS u WHERE u.userPK IN :ids", User.class);
    q.setParameter("ids", ids);
    return q.getResultList();
}

您还可以通过显式遍历嵌入式ID类(userPK.iduserPK.group)进行查询。以下是以下示例:

public List<User> getUsersById(List<Long> ids) {
    TypedQuery<User> q = entityManager.createQuery("SELECT u FROM User AS u WHERE u.userPK.id IN :ids", User.class);
    q.setParameter("ids", ids);
    return q.getResultList();
}

public List<User> getUsersByGroup(List<String> groups) {
    TypedQuery<User> q = entityManager.createQuery("SELECT u FROM User AS u WHERE u.userPK.group IN :groups", User.class);
    q.setParameter("groups", groups);
    return q.getResultList();
}

public User getSingleUserByIdAndGroup(Long id, String group) {
    TypedQuery<User> q = entityManager.createQuery("SELECT u FROM User AS u WHERE u.userPK.id = :id AND u.userPK.group = :group", User.class);
    q.setParameter("id", id);
    q.setParameter("group", group);
    return q.getSingleResult();
}

让我们通过测试每种方法来了解这些查询的行为方式。我们假设我们在USER表上有这些数据。

enter image description here

示例1:按UserPK对象列表查询用户

    List<UserPK> userPKs = new ArrayList<UserPK>();
    userPKs.add(new UserPK(1L, "it"));
    userPKs.add(new UserPK(4L, "eng"));
    List<User> userListQuery1 = userDAO.getUsersByIdCombo(userPKs);
    for (User user: userListQuery1) {
        System.out.println(user);
    }

结果:

ID: 4 | GROUP: eng
ID: 1 | GROUP: it

示例2:按ID列表查询用户

    List<Long> ids = new ArrayList<Long>();
    ids.add(2L);
    ids.add(5L);
    List<User> userListQuery2 = userDAO.getUsersById(ids);
    for (User user: userListQuery2) {
        System.out.println(user);
    }

结果:

ID: 5 | GROUP: eng
ID: 2 | GROUP: it

示例3:按组列表查询用户

    List<String> groups = new ArrayList<String>();
    groups.add("it");
    groups.add("eng");
    List<User> userListQuery3 = userDAO.getUsersByGroup(groups);
    for (User user: userListQuery3) {
        System.out.println(user);
    }

结果:

ID: 4 | GROUP: eng
ID: 5 | GROUP: eng
ID: 1 | GROUP: it
ID: 2 | GROUP: it

示例4:按ID和按组

查询单个用户
    User user = userDAO.getSingleUserByIdAndGroup(3L, "hr");
    System.out.println(user);

结果:

ID: 3 | GROUP: hr