简体   繁体   English

如何在休眠中实现一对一的外键关系映射

[英]how to implement one-to-one foreign key relation mapping in hibernate

I am trying to implement a one to one hibernate relationship mapping and I am new to hibernate technology. 我正在尝试实现one to one hibernate关系mapping而我是休眠技术的新手。 My two entity classes are Employee and Client . 我的两个实体类是EmployeeClient Client should have a employee ID column as foreign key in database table; 客户应该在数据库表中有一个employee ID列作为外键; ie this client is handled by this employee. this客户由this员工处理。 Now there are two jsp pages through which I will submit the details of employee and client. 现在有两个jsp页面,通过这些页面我将提交员工和客户的详细信息。 First I will add/submit employee jsp. 首先,我将添加/提交员工jsp。 Then on client jsp page there would be a select box consisting employeeIDs as its value. 然后在客户端jsp页面上,将有一个包含employeeID作为其值的选择框。 I have done my jsp part. 我已经完成了我的jsp部分。 But I am doubtful of my one to one mapping relationship of client with employee. 但是我怀疑客户与员工之间的一对一关系。 So I am providing my files of code. 所以我提供了我的代码文件。 Below is my code : 下面是我的代码:

employee class: 员工类别:

public class RWEmp {
    private int id; 
    private String strName;
    private String strContactNum;
    private String strDateOfJoining;
    private String strDesignation;
    public RWEmp(){
    }
    // getter/setters
}

client class: 客户类别:

public class RWClient {
    private int id;
    private String strName;
    private RWEmp poc_emp;  // point of contact employee as employee object
    public RWClient(){
    }
    // getter/setters
}

employee.hbm.xml is straight forward. employee.hbm.xml很简单。 ie no relationship. 即没有关系。 But client is having a has a relation with employee object. 但是客户与雇员对象has a关系。

client.hbm.xml: client.hbm.xml:

<hibernate-mapping>
    <class name="com.rightwave.entities.RWClient" table="client_master">
        <id name="id" type="int">
            <generator class="increment" />
        </id>
        <property name="strName" type="string" column="cl_name" />
        <many-to-one name="poc_emp" class="com.rightwave.entities.RWEmp" column="poc_emp" unique="true"></many-to-one>
    </class>
</hibernate-mapping>   

persisting class: 坚持班:

public class PersistEntities {

    public void clientPersist() {
        Session session=Factory.getSession();
        Transaction tr=session.beginTransaction();

        RWEmp rwEmp =new RWEmp();
        rwEmp.setId(2); // this will come from jsp page <select> value. I am doubtful of this, explained below in question.

        RWClient rwClient1=new RWClient("wso2",rwEmp);

        session.save(rwClient1);
        session.flush();
        tr.commit();
        session.close();
    }
}

Here I am not sure if this blueprint is right or wrong. 在这里,我不确定这个蓝图是对还是错。 Can I set the employee ID, which will come from my client jsp page (from in <select> box). 我可以设置员工ID,该ID将来自我的客户端jsp页面(来自<select>框中)。 I am confused because here I am only setting employee ID, which has to be already existing to be a valid foreign key of client. 我很困惑,因为在这里我仅设置员工ID,该ID必须已经存在才能成为客户的有效外键。 But there are no checks of validating whether this employee ID is already existing or not. 但是,没有检查来验证此雇员ID是否已经存在。 The Employee object will definitely be saved (from employee.jsp ) before client object. Employee对象肯定会(从employee.jsp保存)在客户端对象之前。 Am I doing it right way? 我做对了吗?

When establishing one-to-one relationship with two entities both are assigned the same primary key. 与两个实体建立一对一关系时,两个实体都被分配了相同的主键。 A special foreign identifier generator should be declared on Client table to get the primary key value from Employee table. 应该在Client表上声明一个特殊的外部标识符生成器,​​以从Employee表中获取主键值。 Add constrained="true" to ensure the Employee exists. 添加constrained="true"以确保Employee存在。

<hibernate-mapping>
    <class name="com.rightwave.entities.RWClient" table="client_master">        
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="foreign">
                <param name="property">poc_emp</param>
            </generator>
        </id>
        <one-to-one name="poc_emp" class="com.rightwave.entities.RWEmp"
            constrained="true"></one-to-one>
        <property name="strName" type="string" column="cl_name" />        
    </class>
</hibernate-mapping>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM