gpt4 book ai didi

java - 在 hibernate 中使用 Criteria api 如何使用投影实现延迟加载列表?

转载 作者:行者123 更新时间:2023-11-30 11:05:43 25 4
gpt4 key购买 nike

我有三个实体类 Employee 、 ContactDetail 和 ReportingPerson 。这是我的代码,您可以轻松理解它们之间的关系。而且我必须使用 Criteria API 从 Employee 获取 ReportingPerson 的所有详细信息。 Employee 和 ReportingPerson 是一对一的关系。

            @Entity
@Table(name = "tbl_employee")
public class Employee implements Serializable {

/**
*
*/
private static final long serialVersionUID = -3919524684485334176L;

/** The id. */
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

/** The contact details. */
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "tbl_employee_contact", joinColumns = { @JoinColumn(name = "id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "contact_id", nullable = false, updatable = false) })
private List<ContactDetail> contactDetails;

/** The company. */
@Column
private String company;

/** The website. */
@Column
private String website;

@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "person_id", nullable = true)
private ReportingPerson reportingPerson;


@Entity
@Table(name = "tbl_contact")
public class ContactDetail implements Serializable {

/**
*
*/
private static final long serialVersionUID = -3022172440588672233L;

/** The id. */
@Id
@Column(name = "contact_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

/** The type. */
@Column
private String type;

/** The detail. */
@Column
private String detail;

/** The description. */
@Column
private String description;

/** The preferred. */
@Column
private boolean preferred;


@Entity
@Table(name = "tbl_reporting_person")
public class ReportingPerson{

/** The id. */
@Id
@Column(name = "person_id")
@GeneratedValue(generator = "uuid")
private String id;

/** The contact details. */
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "tbl_reporting_person", joinColumns = { @JoinColumn(name = "person_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "contact_id", nullable = false, updatable = false) })
private List<ContactDetail> contactDetails;

/** The company. */
@Column
private String company;

/** The website. */
@Column
private String website;

这是我想要实现的目标,但我得到了 contactDetails 的空值。谁能告诉我哪里错了?

 public ReportingPerson getEmployeeReportingPerson (final String employeeId) {

final Criteria criteria = getDatabaseSession().createCriteria(Employee.class, "employee").createAlias(
"employee.reportingPerson", "person", JoinType.INNER_JOIN);

criteria.add(Restrictions.and(Restrictions.eq("employee.id", employeeId)));

criteria.setProjection(Projections.distinct(Projections.projectionList()
.add(Projections.property("person.id").as("id"))
.add(Projections.property("person.website").as("website"))
.add(Projections.property("person.contactDetails").as("contactDetails"))
.add(Projections.property("person.company").as("company"))));

final ReportingPerson person= (ReportingPerson ) criteria.setResultTransformer(
Transformers.aliasToBean(ReportingPerson .class)).uniqueResult();

return person;
}

最佳答案

关于java - 在 hibernate 中使用 Criteria api 如何使用投影实现延迟加载列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29555308/

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