Hibernate maps one-to-one affinity relationships

Source: Internet
Author: User

Foreign key-based approach:

Attached code:

  

 Public classManager {PrivateInteger Mgrid; PrivateString Mgrname; PrivateDepartment Department;  PublicInteger Getmgrid () {returnMgrid; }     Public voidSetmgrid (Integer mgrid) { This. Mgrid =Mgrid; }     PublicString Getmgrname () {returnMgrname; }     Public voidsetmgrname (String mgrname) { This. Mgrname =Mgrname; }     PublicDepartment getdepartment () {returnDepartment; }     Public voidsetdepartment (Department Department) { This. Department =Department; }}
Manager class
 Public classDepartment {PrivateInteger DeptID; PrivateString Deptname; PrivateManager manager;  PublicInteger Getdeptid () {returnDeptID; }     Public voidSetdeptid (Integer deptid) { This. DeptID =DeptID; }     PublicString Getdeptname () {returnDeptname; }     Public voidsetdeptname (String deptname) { This. Deptname =Deptname; }     PublicManager GetManager () {returnManager; }     Public voidSetmanager (Manager manager) { This. Manager =Manager; }}
Department class
<hibernate-mapping>    <classname= "Hibernate.one2one.foreign.Department"Table= "Departments">        <IDname= "DeptID"type= "Java.lang.Integer">            <columnname= "dept_id" />            <Generatorclass= "Native" />        </ID>        < Propertyname= "Deptname"type= "Java.lang.String">            <columnname= "Dept_name" />        </ Property>        <Many-to-onename= "Manager"class= "Hibernate.one2one.foreign.Manager"Unique= "true"Fetch= "Join">            <columnname= "mgr_id"></column>        </Many-to-one>    </class></hibernate-mapping>
Department.hbm.xml
<hibernate-mapping>    <classname= "Hibernate.one2one.foreign.Manager"Table= "MANAGER">        <IDname= "Mgrid"type= "Java.lang.Integer">            <columnname= "mgr_id" />            <Generatorclass= "Native" />        </ID>        < Propertyname= "Mgrname"type= "Java.lang.String">            <columnname= "Mgr_name" />        </ Property>        <one-to-onename= "department"class= "Hibernate.one2one.foreign.Department"Fetch= "Select"Property-ref= "Manager">                </one-to-one>    </class></hibernate-mapping>
Manager.hbm.xml

where Department maps the Manager using Many-to-one, but needs to set a unique property set to true so that it can only point to one manager

The Manager uses one-to-one to map the Department, noting that a Property-ref property needs to be set because it is associated by default with the primary key in the Department table and needs to set which property it points to Department, note that The value set here is the value in the domain object, not the column name in the data table.

Hibernate maps one-to-one affinity relationships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.