Hibernate 查询在一对多中无法正确投影

发布于 2024-10-31 03:32:38 字数 1225 浏览 3 评论 0原文

我正在执行一个查询,需要返回用户,该查询也填充了 UserConnectors。我的查询不断返回两个结果,而不是单个 ID 的一个结果。它应该是一个结果,但我认为 UserConnector 连接因为用户有两个,所以在结果中创建两个用户,每个用户连接器一个。

用户: ID NAME UserConnector:ID、USERID、DATE

User.java

@OneToMany(mappedBy="user")
     public Set<UserConnector> getUserConnectors() {
        return this.userConnectors;
     }

    /**
     * Set the list of Connectors
     */
     public void setUserConnectors(Set<UserConnector> userConnectors) {
        this.userConnectors = userConnectors;
     }

UserConnector.java

@ManyToOne
@JoinColumn(name = "USERID")
public User getUser() {
    return this.user;
}

/**
 * set users
 */
public void setUser(User user) {
    this.user = user;
}

查询

Criteria criteria = userRepository.getCritieria();

            criteria.createAlias("userConnectors", "uc", Criteria.LEFT_JOIN);

            criteria.add(Restrictions.eq("id", request.getId()));
            criteria.add(Restrictions.eq("enabled", new Long("1")));
            criteria.add(Restrictions.eq("uc.active", new Long("1")));

            List<User> list = userRepository.findByCriteria(criteria);

I am doing a query where I need to get a user back, that also has UserConnectors populated with it. My Query keeps bringing back two results instead of one for a single ID. It should be one result, but I think the UserConnector join since the User has two of them, is creating two Users in the result, one for each User Connector.

Tables

User: ID NAME
UserConnector: ID, USERID, DATE

User.java

@OneToMany(mappedBy="user")
     public Set<UserConnector> getUserConnectors() {
        return this.userConnectors;
     }

    /**
     * Set the list of Connectors
     */
     public void setUserConnectors(Set<UserConnector> userConnectors) {
        this.userConnectors = userConnectors;
     }

UserConnector.java

@ManyToOne
@JoinColumn(name = "USERID")
public User getUser() {
    return this.user;
}

/**
 * set users
 */
public void setUser(User user) {
    this.user = user;
}

Query

Criteria criteria = userRepository.getCritieria();

            criteria.createAlias("userConnectors", "uc", Criteria.LEFT_JOIN);

            criteria.add(Restrictions.eq("id", request.getId()));
            criteria.add(Restrictions.eq("enabled", new Long("1")));
            criteria.add(Restrictions.eq("uc.active", new Long("1")));

            List<User> list = userRepository.findByCriteria(criteria);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三岁铭 2024-11-07 03:32:38

你能试试这个吗?

  List users = session.createCriteria(User.class)
  .add( Restrictions.idEq(request.getId()))
  .add( Restrictions.eq("enabled", new Long("1")))
  .createCriteria("userConnectors")
      .add( Restrictions.like("active", new Long("1")) )
  .list();

Can you try this?

  List users = session.createCriteria(User.class)
  .add( Restrictions.idEq(request.getId()))
  .add( Restrictions.eq("enabled", new Long("1")))
  .createCriteria("userConnectors")
      .add( Restrictions.like("active", new Long("1")) )
  .list();
向地狱狂奔 2024-11-07 03:32:38

与 HQL 一样,对于带有连接的 Criteria,您需要手动触发不同的投影:

 criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

As well as with HQL, for Criteria with joins you need to trigger distinct projection manually:

 criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
美人迟暮 2024-11-07 03:32:38
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文