Criteria.DISTINCT_ROOT_ENTITY不会阻止重复的对象 [英] Criteria.DISTINCT_ROOT_ENTITY doesn't prevent duplicated objects
问题描述
@Override
public List< AdminRole> findAll(){
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(AdminRole.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
其实我想从数据库中检索所有条目。
有时我会看到重复的内容。当我使用AdminRole添加用户时,会发生这种情况。
我读过使用 EAGER
获取类型这应该是修复添加以下行:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
但这并不能帮助我。
<
$ b
@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长adminId;
@Column(name =role)
私人字符串角色;
public AdminRole(String role){
this.role = role;
}
public AdminRole(){
}
//获得集合
@Override
public String toString(){
返回角色;
$ b @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;
}
返回true;
@Override
public int hashCode(){
return role.hashCode();
code
@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;
// ...
}
PS
我无法切换抓取类型。
我不想把这个列表放到集合中。
解决方案没有理由使用 DISTINCT_ROOT_ENTITY
或类似的东西,只需要:
session.createCriteria(AdminRole.class).list();
如果您得到重复项,那么您确实在数据库中有它们。检查直接或通过级联从其他实体保存 AdminRole
s的代码。
当级联 PERSIST
/ MERGE
来自其他实体的操作,请确保该操作级联到持久/分离的 AdminRole
实例,而不是瞬态(新)。
I have following dao method:
@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.
Sometimes I see duplicates. This happens when I add user with AdminRole.
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);
But this doesn't help me.
my mapping:
@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();
}
}
and
@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.
I cannot switch fetch type.
I don't want to put this list into set.
解决方案 There is no reason to use DISTINCT_ROOT_ENTITY
or anything similar, all you need is:
session.createCriteria(AdminRole.class).list();
If you get duplicates, then you really have them in the database. Check the code which saves AdminRole
s either directly or by cascading from other entities.
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屋!