我有三个实体类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的null值。请问有谁可以告诉我哪里错了吗?
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;
}