休眠OneToMany标准返回重复项 [英] hibernate OneToMany criteria returns duplicates

查看:128
本文介绍了休眠OneToMany标准返回重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关联映射如下:

  @Entity 
public class Parent
{
...
@Id
@Column(name =parent_id)
私人长ID;

@OneToMany(mappedBy =parent)
@OrderBy(id)
private List< Child>儿童;
...
}

@实体
公共类儿童
{
...
@Id
@Column(name =child_id)
私人长ID;

@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name =parent_id)
私人父母;

@Column
私有布尔启用;
...
}

我想使用Criteria API返回包含一个或多个 Child 实体且具有属性的所有实体的列表>启用=假。我不希望查询过滤映射的子集集合。

例如,给定以下内容:

 父母A 
- 子女A已启用=真实
- 子女B已启用=假

父母B
- 孩子A启用=假
- 孩子B启用=假

父C
- 子A启用=真$ ​​b $ b - Child B enabled = true

查询应该会返回以下结果:

 父母A 
- 子女A已启用=真实
- 子女B已启用=假

父母B
- Child A enabled = false
- Child B enabled = false

到目前为止,我我使用下面的Criteria查询:

pre $ Criteria crit = session.createCriteria(Parent.class);
crit.createCriteria(children).add(Restrictions.eq(enabled,false));
列表< Parent>结果= crit.list();
返回结果;

然而它返回的是等价的

 父母A 
- 子女A已启用=真实
- 子女B已启用=假

父母B
- 子女A已启用= false
- Child B enabled = false

Parent B
- Child A enabled = false
- Child B enabled = false

即,对于每个启用的子元素,它将返回单个父记录(填充子集合) false



有没有人知道如何在这种情况下返回唯一父元素?



<您需要添加一个不同 >,例如

  criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 

应该适用于您的情况


I have an association mapped by the following:

@Entity
public class Parent
{
...
    @Id
    @Column(name = "parent_id")
    private Long id;

    @OneToMany(mappedBy = "parent")
    @OrderBy("id")
    private List<Child> children;
...
}

@Entity
public class Child
{
...
    @Id
    @Column(name = "child_id")
    private Long id;

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "parent_id")
    private Parent parent;

    @Column
    private Boolean enabled;
...
}

I would like to use the Criteria API to return a list of all of the Parent entities which contain one or more Child entities with the attribute enabled=false. I would not like the mapped children collection to be filtered by the query.

For example, given the following:

Parent A
    - Child A enabled=true
    - Child B enabled=false

Parent B
    - Child A enabled=false
    - Child B enabled=false

Parent C
    - Child A enabled=true
    - Child B enabled=true

The query should return the following:

Parent A
    - Child A enabled=true
    - Child B enabled=false

Parent B
    - Child A enabled=false
    - Child B enabled=false

So far I am using the following Criteria query:

Criteria crit = session.createCriteria(Parent.class);
crit.createCriteria("children").add(Restrictions.eq("enabled", false));
List<Parent> result = crit.list();
return result;

However it is returning the equivalent of

Parent A
    - Child A enabled=true
    - Child B enabled=false

Parent B
    - Child A enabled=false
    - Child B enabled=false

Parent B
    - Child A enabled=false
    - Child B enabled=false

Ie, it is returning a single parent record (with the children collection populated) for each child element with enabled=false

Does anyone know how to only return unique parent elements in this scenario?

Advice appreciated, p.

解决方案

You need to add a distinct, e.g.

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

should work in your case

这篇关于休眠OneToMany标准返回重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆