Criteria.DISTINCT_ROOT_ENTITY不能防止对象重复 [英] Criteria.DISTINCT_ROOT_ENTITY doesn't prevent duplicated objects

查看:74
本文介绍了Criteria.DISTINCT_ROOT_ENTITY不能防止对象重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下dao方法:

@Override
public List<AdminRole> findAll() {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(AdminRole.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

实际上我想从数据库中检索所有条目.

Actually I want to retrieve all entries from database.

有时候我看到重复的东西.当我使用AdminRole添加用户时,就会发生这种情况.

Sometimes I see duplicates. This happens when I add user with AdminRole.

我已经读到,当我使用 EAGER 提取类型时,这是可能的,应该在添加以下代码后解决:

I have read that it is possible when I use EAGER fetch type and this should be fix adding following line:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

但这对我没有帮助.

我的映射:

@Entity
@Table(name = "terminal_admin_role")
public class AdminRole {

    @Id
    @Column(name = "role_id", nullable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id")
    @SequenceGenerator(name = "user_id", sequenceName = "user_id")
    private Long adminId;

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

    public AdminRole(String role) {
        this.role = role;
    }

    public AdminRole() {
    }

    // get set

    @Override
    public String toString(){
        return role;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof AdminRole)) {
            return false;
        }

        AdminRole adminRole = (AdminRole) o;

        if (!role.equals(adminRole.role)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return role.hashCode();
    }
}

@Entity
@Table(name = "terminal_admin")
public class TerminalAdmin {
    @ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinTable(name = "admin_role", joinColumns = { 
        @JoinColumn(name = "admin_id", nullable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "role_id", 
                nullable = false) })
    private Set<AdminRole> adminRoles;      
    //...
}

P.S.

我无法切换获取类型.

P.S.

I cannot switch fetch type.

我不想把这个列表放进去.

I don't want to put this list into set.

推荐答案

没有理由使用 DISTINCT_ROOT_ENTITY 或类似的东西,您所需要的只是:

There is no reason to use DISTINCT_ROOT_ENTITY or anything similar, all you need is:

session.createCriteria(AdminRole.class).list();

如果您有重复项,那么您实际上会将它们存储在数据库中.检查直接保存 AdminRole 或从其他实体级联保存 AdminRole 的代码.

If you get duplicates, then you really have them in the database. Check the code which saves AdminRoles either directly or by cascading from other entities.

从其他实体级联 PERSIST / MERGE 操作时,请确保该操作级联到持久/分离的 AdminRole 实例,而不是过渡到瞬态(新).

When cascading PERSIST/MERGE operations from other entities, make sure that the operation is cascaded to a persistent/detached AdminRole instance, not to a transient (new) one.

这篇关于Criteria.DISTINCT_ROOT_ENTITY不能防止对象重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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