使用条件获取单向一对多关系中的子类,该关系处于休眠状态,与 JoinTable 一起



我有两个实体类员工和联系人详细信息,员工类具有一对多关系,具有可联合注释的单向关系。我的问题是,是否可以从特定员工那里获得所有联系人列表?这是我的代码,你很容易理解

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

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

并且我懒惰地获取联系人详细信息,因此我必须在 dao 层中制作另一种方法,以便我可以从特定员工那里获取所有联系人。我想要这样的东西

public List<ContactDetail> getContactList(string employeeId) {
        final Criteria criteria = HibernateUtil.getSessionFactory()
                .getCurrentSession()
                .createCriteria(ContactDetail.class, "contact");
        //here some code 
        return contactList;
    }

您可以直接从员工实例获取联系人列表。

一旦你通过执行session.load或其他任何事情获得Employee实例,你就可以做

List<ContactDetail> lContactDetails = employee.getContactDetails()

您不需要执行此操作的标准。

最新更新