我有以下道法:
@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);
但这并不能帮助我.
我的映射:
@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; //... }
附:
我无法切换提取类型.
我不想把这个列表放在一起.