Hibernate one-to-one mapping

Source: Internet
Author: User

As long as you work hard, there is nothing you can't screw up.



Requirement: an automobile (car) has an engine, which is a one-to-one mapping relationship.

There are two ways to implement a one-to-one mapping: foreign-key-based mapping and primary-key-based mapping. The focus of both of these approaches is the configuration of the mapping file, which is recommended in the first way.

1. Mapping based on foreign keys

Car.java

Package Com.rk.hibernate.k_one2one;public class Car{private int carid;private String carname;private engine engine; public int Getcarid () {return carid;} public void Setcarid (int carid) {this.carid = Carid;} Public String Getcarname () {return carname;} public void Setcarname (String carname) {this.carname = Carname;} Public engine Getengine () {return engine;} public void Setengine (engine engine) {this.engine = engine;} @Overridepublic String toString () {return "Car [carid=" + Carid + ", carname=" + Carname + "]";}}

Car.hbm.xml, note the mapping of Many-to-one

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

Engine.java

Package Com.rk.hibernate.k_one2one;public class Engine{private int engineid;private String enginename;private car car; public int Getengineid () {return engineid;} public void Setengineid (int engineid) {This.engineid = Engineid;} Public String Getenginename () {return enginename;} public void Setenginename (String enginename) {this.enginename = enginename;} Public Car Getcar () {return car;} public void Setcar (car car) {This.car = car;} @Overridepublic String toString () {return "Engine [engineid=" + Engineid + ", enginename=" + Enginename + "]";}}

Engine.hbm.xml, note one-to-one mapping

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

App.java

package com.rk.hibernate.k_one2one;import org.hibernate.session;import  Org.hibernate.sessionfactory;import org.hibernate.cfg.configuration;import org.junit.test;public  class App{private static SessionFactory sf;static{sf = new  Configuration (). Configure (). addclass (Car.class). addclass (Engine.class). Buildsessionfactory (); @Testpublic  void testsave () {int i = 2; Session session = sf.opensession (); session.begintransaction ();//Engine engine engine =  new engine (); Engine.setenginename ("Super engine"  + i);//Car car car =  New car (), Car.setcarname ("BMW"  + i), Car.setengine (engine),  //car and engine relationship session.save (engine); Session.save (car); Session.gettransaction (). commit (); Session.close ();} @Testpublic  void testget () {session session = sf.opensession (); Session.begintransaction ();//car car =  (Car) session.get(car.class, 2);//system.out.println (Car);//engine engine = car.getengine ();// SYSTEM.OUT.PRINTLN (engine); engine e2 =  (Engine) session.get (engine.class, 3); SYSTEM.OUT.PRINTLN (E2); Car c2 = e2.getcar (); System.out.println (C2); Session.gettransaction (). commit (); Session.close ();}}



2. Primary key-based mapping

Car.java

Package Com.rk.hibernate.l_one2one_primarykey;public class Car{private int carid;private String carname;private Engine engine;public int Getcarid () {return carid;} public void Setcarid (int carid) {this.carid = Carid;} Public String Getcarname () {return carname;} public void Setcarname (String carname) {this.carname = Carname;} Public engine Getengine () {return engine;} public void Setengine (engine engine) {this.engine = engine;} @Overridepublic String toString () {return "Car [carid=" + Carid + ", carname=" + Carname + "]";}}

Car.hbm.xml, note one-to-one configuration

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

Engine.java, note that the Carid property of the current class is the primary key of the T_engine2 table and also the foreign key of the t_car2.

Package Com.rk.hibernate.l_one2one_primarykey;public class Engine{private int carid;private String enginename;private Car car;public int Getcarid () {return carid;} public void Setcarid (int carid) {this.carid = Carid;} Public String Getenginename () {return enginename;} public void Setenginename (String enginename) {this.enginename = enginename;} Public Car Getcar () {return car;} public void Setcar (car car) {This.car = car;} @Overridepublic String toString () {return "Engine [carid=" + Carid + ", enginename=" + Enginename + "]";}}

Engine.hbm.xml, note the configuration of the ID and one-to-one.

<?xml version= "1.0"  encoding= "UTF-8"? ><! doctype hibernate-mapping public  "-//hibernate/hibernate mapping dtd 3.0//en" " Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">

app.java

package com.rk.hibernate.l_one2one_primarykey;import org.hibernate.session;import  Org.hibernate.sessionfactory;import org.hibernate.cfg.configuration;import org.junit.test;public  class App{private static SessionFactory sf;static{sf = new  Configuration (). Configure (). addclass (Car.class). addclass (Engine.class). Buildsessionfactory (); @Testpublic  void testsave () {int i = 0; Session session = sf.opensession (); session.begintransaction ();//Car car car = new  car (); Car.setcarname ("BMW"  + i);//engine engine engine = new engine (); Engine.setenginename ("Super engine"  + i); Engine.setcar (car); Session.save (car); Session.save ( engine); Session.gettransaction (). commit (); Session.close ();} @Testpublic  void testget () {session session = sf.opensession (); Session.begintransaction (); car car =  (CAR) session.get (car.class,  1); SYSTEM.OUT.PRINTLN (car); Engine engine = car.getengine (); SYSTEM.OUT.PRINTLN (engine);//engine e2 =  (engine) session.get (engine.class, 1);// SYSTEM.OUT.PRINTLN (E2);//car c2 = e2.getcar ();//system.out.println (C2); Session.gettransaction () . commit (); Session.close ();}}




Hibernate one-to-one mapping

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.