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

时间:2016-02-21 04:03:36

标签: java hibernate jsp hibernate-mapping

我正在尝试实施one to one hibernate关系mapping,我是hibernate技术的新手。我的两个实体类是EmployeeClient。客户端应该在数据库表中有一个employee ID列作为外键;即this客户由this员工处理。现在有两个jsp页面,我将通过它提交员工和客户的详细信息。首先,我将添加/提交员工jsp。然后在客户端jsp页面上会有一个包含employeeID作为其值的选择框。我做了我的jsp部分。但我怀疑客户与员工之间的一对一映射关系。所以我提供了我的代码文件。以下是我的代码:

员工班级:

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

客户类:

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很直接。即没有关系。 但客户端与员工对象有has a的关系。

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>   

坚持上课:

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

我不确定这个蓝图是对还是错。我是否可以设置员工ID,该ID将来自我的客户端jsp页面(来自<select>框)。我很困惑,因为在这里我只设置员工ID,必须已经存在才能成为客户的有效外键。但是没有检查验证该员工ID是否已经存在。 Employee对象肯定会在客户端对象之前保存(从employee.jsp)。我这样做了吗?

1 个答案:

答案 0 :(得分:0)

当与两个实体建立一对一关系时,两者都被分配了相同的主键。应在import UIKit let IMAGE_OVERLAY_NAME = "backgroundColorExample" class YOUTransparentModalVC: UIViewController { private var backgroundImageOverlayIV:UIImageView! override func viewDidLoad() { super.viewDidLoad() self.setupTransparentView() self.setupDissmissingVCOnTap() self.setupBackgroudImage() } private func setupTransparentView() { self.view.backgroundColor = UIColor.clearColor() let effect = UIBlurEffect(style: UIBlurEffectStyle.Light) let blurView = UIVisualEffectView(effect: effect) blurView.frame = self.view.bounds self.view.addSubview(blurView) } private func setupBackgroudImage() { self.backgroundImageOverlayIV = UIImageView(frame: self.view.frame) self.backgroundImageOverlayIV.image = UIImage(named: IMAGE_OVERLAY_NAME) self.view.addSubview(self.backgroundImageOverlayIV) } private func setupDissmissingVCOnTap() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissVC") view.addGestureRecognizer(tap) } func dismissVC() { self.dismissViewControllerAnimated(true, completion: nil) } } 表上声明一个特殊的外部标识符生成器,​​以从Client表中获取主键值。添加Employee以确保constrained="true"存在。

Employee