我有三个实体类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;
            }

最佳答案

我希望这可以解决您的查询

https://github.com/samiandoni/AliasToBeanNestedResultTransformer

10-06 09:12