One, combined mapping
The combination is a special case of association relation, it is the relationship with the highest coupling degree of association relation, the combination main object and child object have the same life cycle, and the child object of the death of the Lord is extinct. Here use the employer and the user as an example, the user and the employer have the contact method attribute, if here stand in the object angle of thinking, often will draw the object model into a combination of way, abstract out a common contact way class, then two kinds of people contain the corresponding contact method object, The object model for Shang is shown in the following illustration:
The composite object model will include the corresponding subclass in the primary table after the corresponding relational model is generated, so the corresponding table structure will generate the corresponding property into the corresponding table structure as follows:
1.1 Employee class and mapping file
There is an inclusion relationship between employee and contact in the object model, and you need to include the Contacts object in employee when you write code. The corresponding mapping file also requires a mapping of the contact object, which uses the <component> tag to indicate the grouped object and adds the object's properties to the object tag.
Listing I: Employee.java, in addition to the basic attributes in the class file, you need to separate the contact objects, because there is a layer of relationship between them.
Package com.src.hibernate;
public class Employee {
//id number
private int id;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Name
private String name;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Contact the object
private contacts usercontact;
Public Contacts Getusercontact () {return
usercontact;
}
public void Setusercontact (contact usercontact) {
this.usercontact = usercontact;
}
}
Listing two: Employee.hbm.xml, add the corresponding mapping file, the mapped composite object to be marked with <component>, and add the corresponding object attributes in the tag, the following code:
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
1.2 User class and configuration file
Listing three: User.java, its content structure and employee.java the same, others no longer say, look at the code:
Package com.src.hibernate;
public class User {
//id number
private int id;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Name
private String name;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Contact the object
private contacts usercontact;
Public Contacts Getusercontact () {return
usercontact;
}
public void Setusercontact (contact usercontact) {
this.usercontact = usercontact;
}
}
Listing four: User.hbm.xml, its content structure with Employee.hbm.xml content, mainly is the use of <component> tags, very simple, the code is as follows:
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
1.3 Contact.java Class
the class file has nothing to be aware of, add the basic attributes, and do not need to configure the corresponding mapping for the class, so its content is fairly simple.
Package com.src.hibernate;
public class Contacts {
//email address
private String email;
Public String Getemail () {return
email;
}
public void Setemail (String email) {
This.email = email;
}
Address
private String addresses;
Public String getaddress () {return address
;
}
public void setaddress (String address) {
this.address = address;
}
Mail number
private String zipCode;
Public String Getzipcode () {return
zipCode;
}
public void Setzipcode (String zipCode) {
this.zipcode = ZipCode;
}
Contact telephone
private String Contacttel;
Public String Getcontacttel () {return
Contacttel;
}
public void Setcontacttel (String contacttel) {
This.contacttel = Contacttel;
}
}
1.4 Build Results
after the above file configuration, you can then generate the corresponding database table structure, the resulting SQL statement is as follows:
drop table if exists t_employee
drop table if exists t_user
CREATE TABLE t_employee (ID integer NOT NULL AUTO_INCR Ement, name varchar (255), email varchar (255), address varchar (255), ZipCode varchar (255), Contacttel varchar (255), Primar Y key (ID)
CREATE TABLE T_user (ID integer NOT NULL auto_increment, name varchar (255), email varchar (255), address VA Rchar (255), ZipCode varchar (255), Contacttel varchar (255), primary key (ID)
The resulting database table structure is as follows:
Second, data operation
the table structure that the composite map obtains is a complete table, so the most original method can be implemented when writing and reading the data, and the test methods used in the previous articles are used to write and read the data, respectively, using the Save and load methods, see below for specific actions.
2.1 Inserting data
here, using user as an example, the write operation of employee is the same as user. When you write data, you need to create two objects, one is the contact object, the other is the user object, save the user only need to save the object, the corresponding contact object will be linked to save.
public void TestSave1 () {
//declaring Session object sessions
Session=null;
try{
//Get Session Object
session=hibernateutils.getsession ();
Open session
session.begintransaction ();
Create a Connection object contact
usercontact=new contacts ();
Usercontact.setaddress ("Beijing");
Usercontact.setcontacttel ("1243435");
Usercontact.setemail ("123@gamil.com");
Usercontact.setzipcode ("ZipCode");
Create user object username
user=new User ();
User.setname ("Zhangsan");
User.setusercontact (usercontact);
Session.save (user);
Submit Session
Session.gettransaction (). commit ();
catch (Exception e) {
e.printstacktrace ();
Session.gettransaction (). rollback ();
} finally{
Hibernateutils.closesession (session);
}
Generated SQL statement:
Insert into T_user (name, email, address, zipCode, Contacttel) VALUES (?,?,?,?,?)
Look at the table structure as follows:
2.2 Read operations
Similarly, using user as an example, the employee action is the same as the user object. The read operation is fairly simple, and the code is as follows:
public void TestLoad1 () {
//declaring Session object sessions
Session=null;
try{
//Get Session Object
session=hibernateutils.getsession ();
Open session
session.begintransaction ();
Gets the user object, user
user= (user) Session.load (user.class, 1);
System.out.println ("User name:" +user.getname ());
Submit Session
Session.gettransaction (). commit ();
catch (Exception e) {
e.printstacktrace ();
Session.gettransaction (). rollback ();
} finally{
Hibernateutils.closesession (session);
}
The resulting results correspond to the following:
Hibernate:select user0_.id as id0_0_, user0_.name as name0_0_, User0_.email as email0_0_, user0_.address as address0_0_, User0_.zipcode as zipcode0_0_, User0_.contacttel as contacttel0_0_ from T_user user0_ where user0_.id=?
User name: Zhangsan
Iii. Comprehensive Examples
Account:
Public class account implements serializable{
private int id;
private double;
private address address;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public double Getmoney () {return money
;
}
public void Setmoney (double) {
This.money = money;
}
Public Address getaddress () {return address
;
}
public void setaddress [address] {
this.address = address;
}
}
Address:
Public class address implements serializable{
private String code;
Private String city;
Private String Province;
Public String GetCode () {return
code;
}
public void Setcode (String code) {
this.code = code;
}
Public String getcity () {return city
;
}
public void Setcity (String city) {
this.city = city;
}
Public String getprovince () {return
province;
}
public void Setprovince (String province) {
this.province = province;
}
}
Account.hbm.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "> <!--mapping file autogenerated by MyEclipse persistence Tools-->