Hibernate 1-1 (Detailed)

Source: Internet
Author: User
Tags class manager sql using

Domain model


Relational Data Model:

    • Follow foreign key mappings:

    • Follow the primary key mappings:


1-1 based on foreign key mappings
    • For 1-1 associations based on the foreign key, the foreign key can be stored on either side, and the many-to-one element should be added at the end of the external key . Adds a unique= "true" property to the Many-to-one element to represent a 1-1 association

    • The other end needs to use the one-to-one element, which uses the Property-ref property to specify that a field other than the primary key of the associated entity be used as the associated field

-SQL that does not use the Property-ref property

-SQL using the Property-ref property


Why not use 1-1 of foreign key mappings on both sides


This is not a one-to-one relationship mapping.


1-1 based on the primary key mapping

    • Primary key-based mapping strategy: The primary key generator at one end uses the foreign policy, indicating that the primary key is generated based on the primary key of the "opponent", and that the primary key cannot be generated independently. <param> child element specifies which property of the current persisted class is used as the "opponent"

    • The one-to-one element Map Association attribute is added at one end of the foreign primary key generator policy, and its one-to-one property should also be added constrained= "true", and the other end adds one-to-one element mapping associated properties.
    • Constrained (constraint): Specifies that a FOREIGN key constraint be added for the primary key of the database table corresponding to the current persisted class, referencing the database table primary key for the associated object (" opposite party ")



1-1 based on foreign key mappings

Detailed Examples:

Department.java

Package Com.atguigu.hibernate.one2one.foreign;public class Department {private Integer deptid;private String deptname; Private Manager mgr;public Integer Getdeptid () {return deptid;} public void Setdeptid (Integer deptid) {this.deptid = DeptID;} Public String Getdeptname () {return deptname;} public void Setdeptname (String deptname) {this.deptname = Deptname;} Public Manager Getmgr () {return MGR;} public void Setmgr (Manager mgr) {this.mgr = mgr;}}


Manager.java
Package Com.atguigu.hibernate.one2one.foreign;public class Manager {private Integer mgrid;private String mgrname; Private Department dept;public Integer Getmgrid () {return mgrid;} public void Setmgrid (Integer mgrid) {this.mgrid = Mgrid;} Public String Getmgrname () {return mgrname;} public void Setmgrname (String mgrname) {this.mgrname = Mgrname;} Public Department getdept () {return dept;} public void Setdept (Department dept) {this.dept = dept;}}


Department.hbm.xml
<?xml Version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


Manager.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Package Com.atguigu.hibernate.one2one.foreign;import Java.io.fileinputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.sql.blob;import Java.sql.connection;import Java.sql.sqlexception;import Java.util.date;import Org.hibernate.hibernate;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.jdbc.work;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.After ; Import Org.junit.before;import Org.junit.test;public class Hibernatetest {private Sessionfactory sessionfactory; Private Session session;private Transaction Transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure ();                            Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry (); sessionfactory = Configuration.buildSessionfactory (serviceregistry); session = Sessionfactory.opensession (); transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} @Testpublic void TestGet2 () {//When querying an entity object that does not have a foreign key, the left OUTER join query is queried for its associated object//and has been initialized. Manager mgr = (manager) Session.get (Manager.class, 1); System.out.println (Mgr.getmgrname ()); System.out.println (Mgr.getdept (). Getdeptname ()); } @Testpublic void Testget () {//1. By default, lazy loading Department dept = (Department) Session.get for associated properties (Department.class, 1); System.out.println (Dept.getdeptname ()); 2. So there is the problem of lazy loading exception. Session.close ();//manager mgr = Dept.getmgr ();//system.out.println (Mgr.getclass ()); System.out.println (Mgr.getmgrname ()); 3. The connection condition of the query Manager object should be dept.manager_id = mgr.manager_id//instead of dept.dept_id = Mgr.manager_idmanager mgr = Dept.getmgr (); System.out.println (Mgr.getmgrname ()); } @Testpublic void Testsave () {Department Department = new Department ();d epartment.setdeptname ("Dept-bb"); Manager ManageR = new Manager (); Manager.setmgrname ("MGR-BB");//Set Association relationship department.setmgr (Manager); manager.setdept (department);// Save operation//It is recommended that the object with no foreign key columns be saved first. This will reduce the UPDATE statement Session.save (department); Session.save (manager);}}


1-1 based on the primary key mapping

Detailed Examples:

The entity class does not change the same as above

Department.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


Manager.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Package Com.atguigu.hibernate.one2one.primary;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.After ; Import Org.junit.before;import Org.junit.test;public class Hibernatetest {private Sessionfactory sessionfactory; Private Session session;private Transaction Transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure ();                            Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry (); sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Session Factory.opensession (); transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} @Testpublic void TestGet2 () {//When querying for entity objects that do not have foreign keys, Use the left OUTER join query to query out its associated object//and initialize it. Manager mgr = (manager) Session.get (Manager.class, 1); System.out.println (Mgr.getmgrname ()); System.out.println (Mgr.getdept (). Getdeptname ()); } @Testpublic void Testget () {//1. By default, lazy loading Department dept = (Department) Session.get for associated properties (Department.class, 1); System.out.println (Dept.getdeptname ()); 2. So there is the problem of lazy loading exception. Manager mgr = Dept.getmgr (); System.out.println (Mgr.getmgrname ()); } @Testpublic void Testsave () {Department Department = new Department ();d epartment.setdeptname ("Dept-dd"); Manager Manager = new manager (); Manager.setmgrname ("Mgr-dd");//Set Correlation manager.setdept (department);d Epartment.setmgr (manager);//save operation//First insert which will not have redundant Updatesession.save (department); Session.save (manager);}}


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.