Component Mappings
The mapping of class composition relationships, also called component mappings!
Note: The component class and the included component classes are mapped together into a table!
Requirements: such as automobiles and wheels
code example:
1, JavaBean
Wheel.java
Package com.gqx.component;/** * Wheels * @author Guo Qingxing * */public class Wheel {private int count;private int size;public int get Count () {return count;} public void SetCount (int count) {This.count = count;} public int GetSize () {return size;} public void setSize (int size) {this.size = size;}}
Car.java
Package Com.gqx.component;public class Car {private int id;private String brand;//wheel private Wheel wheel;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getbrand () {return brand;} public void Setbrand (String brand) {This.brand = brand;} Public Wheel Getwheel () {return Wheel;} public void Setwheel (Wheel Wheel) {this.wheel = Wheel;}}
2. mapping files
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
3. Test procedure
Package Com.gqx.component;import static Org.junit.assert.*;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.afterclass;import Org.junit.test;import Com.gqx.collection.user;public class App {private static sessionfactory sf;static{sf=new Configuration (). Configure (). addclass (Car.class). Buildsessionfactory (); @Testpublic void Test () {Session session=sf.opensession (); session.begintransaction ();//Wheel Wheel wheel=new wheel (); Wheel.setcount (4); wheel.setsize (40);//auto car Car car=new car () Car.setbrand ("BMW"); Car.setwheel (wheel);// Save Session.save (car); Session.gettransaction (). commit (); Session.close ();}}
Inheritance Mappingssuch as: The parent class--Animal
Subclass: Cat, Monkey
1. javabean file
Animal.java
Package com.gqx.extends1;/** * Animal class * @author Guo Qingxing * */public abstract class Animal {private int id;private String NAME;PUBL IC int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Cat.java
Package Com.gqx.extends1;public class Cat extends Animal {//Catch mouse private string Catchmouse;public string Getcatchmouse () {R Eturn Catchmouse;} public void Setcatchmouse (String catchmouse) {this.catchmouse = Catchmouse;}}
Monkey.java
Package Com.gqx.extends2;public class Monkey extends animal{//eat banana private string Eatbanana;public string Geteatbanana () { return Eatbanana;} public void Seteatbanana (String eatbanana) {This.eatbanana = Eatbanana;}}
2. mapping files
Simple Inheritance Mappings<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
3. Test files
Package Com.gqx.extends1;import static Org.junit.assert.*;import Java.util.list;import Org.hibernate.query;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.afterclass;import Org.junit.test;import Com.gqx.collection.user;public class App {private static Sessionfactory sf;static{sf=new Configuration (). Configure (). addclass (Cat.class). Buildsessionfactory (); @Testpublic void Test () {Session session=sf.opensession (); Session.begintransaction (); Cat Cat=new Cat (), Cat.setname ("Dragon Cat"), Cat.setcatchmouse ("Catch Mouse"), Session.save (cat); Session.gettransaction (). Commit ( ); Session.close ();} @Testpublic void Testget () {Session session=sf.opensession (); Session.begintransaction ();//query q= Session.createquery ("from Cat");//list<cat> list=q.list ();//If you query through the parent class, you need to explain the package query Q=session.createquery ("from Com.gqx.extends1.Cat "); List<animal> list=q.list (); SYSTEM.OUT.PRINTLN (list); Session.gettransaction (). commit (); Session.close ();}}
Mapping files can also be divided into three types to implement one, all sub-classes mapped to a table (1 tables)What's the use of?
Subclasses teach many, and subclasses are simpler, that is, only individual attributes!
Benefit: Because a mapping file is used, the number of mapping files is reduced.
Cons: (not conforming to database design principles)
A mapping file: Animal.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "><!--inheritance mappings, all subclasses mapped to a table-->Results
Summarize:
It's simpler: all subclasses use a mapping file and map to a table!
But the database design is unreasonable!
Two, each class mapping a table (3 tables)Database
T_anmal (Storing parent class information)
T_cat (refers to the primary key of the parent class)
T_monkey (refers to the primary key of the parent class)
mapping files
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "><!--inheritance mappings, one table for each class (the parent class also corresponds to a table)-->
Summarize:
A mapping file that stores all subclasses, and the parent classes of subclasses are the corresponding tables;
Disadvantage: Table structure is responsible for inserting a subclass of information, you need to use 2 sql: INSERT into the parent class, insert into the child class!
Three, (recommended) each sub-class mapping a table, the parent class does not correspond to the table (2 tables)This time to change the ID of the animal class.
Private String ID;
mapping files
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "><!--inheritance mappings, one table per subclass (parent class does not correspond to a table)-->
Primary key mapping and hibernate mapping