gpt4 book ai didi

java - 当使用额外条件运行分页查询时,Hibernate Mapping 返回多个相同的对象

转载 作者:行者123 更新时间:2023-11-29 08:07:13 24 4
gpt4 key购买 nike

所以我一直在这方面做不到,而且似乎无法让它正常工作。

当我使用我的 HibernateUtil.get(clazz, pkId) 方法时似乎一切正常,但是当我尝试使用 HibernateUtil.pagedQuery(clazz, criterion, start, stop) 我返回了多个相同的对象。

例如,如果有 3 名员工分配到角色 1,则运行...

Role role = HibernateUtil.get(Role.class, new Integer(1));

... 按预期工作。但是,如果我运行...

List<Criterion> c = new ArrayList();
c.add(Restrictions.eq("roleTypeSeqNo", new Integer(1)));
List<Role> roles = (List<Role>) phi.util.hibernate.HibernateUtil.pagedQuery(Role.class, c, 0, 50);

... 返回 3 个相同角色的列表。所有这些都代表角色 1。

如果有人能引导我走上正确的道路,我将不胜感激。

提前致谢!

这是我的类(class)的缩略版

@Entity
@Table(name="ASSIGNMENTS")
public class Assignment implements Serializable {
@EmbeddedId
private AssignmentPK pk;
// After coming across many errors I finally caved and reverted roleTypeSeqNo back to just an Integer.
private Integer roleTypeSeqNo;
private String status;
private String location;
}

@Embeddable
public class AssignmentPK implements Serializable {
@ManyToOne
@JoinColumn(name="EMPLID")
private Employee employee;
@Column(name="PRIORITY_NO")
private String priorityNo;
}

@Entity
public class Employee implements Serializable {
@Id
private Integer emplId;
private String name;
}

@Entity
public class Role implements Serializable {
@Id
private Integer roleTypeSeqNo;
private Integer reportsToRole;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="roleTypeSeqNo")
@JoinTable(
name="ASSIGNMENTS"
, joinColumns={@JoinColumn(name="ROLE_TYPE_SEQ_NO")}
, inverseJoinColumns={
@JoinColumn(name="EMPLID"),
@JoinColumn(name="PRIORITY_NO")
}
)
private List<Assignment> assignments;
}

public class HibernateUtil {
public static Object get(Class clazz, Serializable pkId) {
Session session = getSession();
Transaction transaction = session.beginTransaction();

Object obj = session.get(clazz, pkId);

transaction.commit();
session.close();

return obj;
}

public static List pagedQuery(Class clazz, List<Criterion> criterion, Integer start, Integer size){
Session session = getSession();
try {
Transaction transaction = session.beginTransaction();

DetachedCriteria dCriteria = DetachedCriteria.forClass(clazz);
for(Criterion c : criterion){
dCriteria.add(c);
}
dCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
dCriteria.setProjection(Projections.id());

Criteria criteria=session.createCriteria(clazz);
criteria.add(Subqueries.propertyIn("id", dCriteria));
criteria.setFirstResult(start);
criteria.setMaxResults(size);

List records = criteria.list();

transaction.commit();

return records;
} catch (Exception e) {
Logger.getLogger("HibernateUtil").log(Level.SEVERE, "There was an EXCEPTION THROWN!!!", e);
return null;
} finally {
session.close();
}
}
}

最佳答案

dCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

应该在主要标准上

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

也不需要子查询。以下就够了

    Criteria criteria = session.createCriteria(clazz);

for(Criterion c : criterions){
criteria.add(c);
}

criteria.setFirstResult(start);
criteria.setMaxResults(size);

List records = criteria.list();

关于java - 当使用额外条件运行分页查询时,Hibernate Mapping 返回多个相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10325259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com