【问题标题】:Using Criteria api in hibernate how can I achieve Lazy loaded list using projection?在休眠中使用 Criteria api 如何使用投影实现延迟加载列表?
【发布时间】:2015-04-10 07:03:05
【问题描述】:

我有三个实体类 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 orm hibernate-criteria


    【解决方案1】:

    【讨论】:

    • 感谢您的帮助,但在此链接中假设汽车有一个像 List 灯这样的成员,并且它有一对多的关系,那么我怎样才能获得灯的列表。我希望你能理解。
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多