在休眠中使用条件 api,如何使用投影实现延迟加载列表



我有三个实体类 员工 , 联系人详细信息 和 报告人.这是我的代码,它们之间的关系你很容易理解。我必须使用标准 API 从员工那里获取报告人的所有详细信息。员工和报告人有一对一的关系。

            @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;

这是我试图实现的目标,但我得到的联系人详细信息为空值。请任何人都可以告诉我我错在哪里?

 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;
            }

我希望这能解决您的查询

https://github.com/samiandoni/AliasToBeanNestedResultTransformer

最新更新