1. Combination mapping
Combination is a special case of association relationships and a relationship with the highest degree of coupling. The main object and the sub-object of the combination have the same life cycle. If the main object dies, the sub-object will also die. Here we use employers and users as examples. Both users and employers have contact information attributes. If you think from the perspective of an object, you will often draw the object model into a combination method to abstract a common contact information class. Then the two people include corresponding contact information objects. When facing the corresponding object model, its object example is shown in the figure below:
After the combined object model generates the corresponding relationship model, the corresponding subclass will be included in the main table, so the corresponding table structure will generate the corresponding attributes into the corresponding table. The corresponding table structure is as follows:
1.1 Employee class and mapping files
In the object model, there is an inclusion relationship between Employee and Contact. When writing code, you need to include the Contact object in Employee. The corresponding mapping file also needs to have a mapping of Contact objects. The <component> tag needs to be used to indicate the combined objects and add the object properties to the object tag.
Listing: Employee.java. In addition to basic properties, the class file also needs to be packaged with Contact objects because there is a layer of inclusion 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 object private Contact userContact; public Contact getUserContact() { return userContact; } public void setUserContact(Contact userContact) { this.userContact = userContact; } }Listing 2: Employee.hbm.xml, add the corresponding mapping file. The mapped combination objects should be marked with <component>, and add the corresponding object attributes to the tag. The specific 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"> <hibernate-mapping> <class name="com.src.hibernate.Employee" table="t_employee"> <id name="id"> <generator/> </id> <property name="name"/> <component name="employeeContact"> <property name="email"/> <property name="address"/> <property name="zipCode"/> <property name="contactTel"/> </component> </class> </hibernate-mapping>
1.2 User class and configuration files
Listing 3: User.java, its content structure is the same as that of Employee.java. I won’t say much about the others, 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 object private Contact userContact; public Contact getUserContact() { return userContact; } public void setUserContact(Contact userContact) { this.userContact = userContact; } }Listing 4: User.hbm.xml, its content structure is the same as that of Employee.hbm.xml, and it is mainly used for the use of the <component> tag. It is very simple, and 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"> <hibernate-mapping> <class name="com.src.hibernate.User" table="t_user"> <id name="id"> <generator/generator/> </id> <property name="name"/> <component name="userContact"> <property name="email"/> <property name="address"/> <property name="zipCode"/> <property name="contactTel"/> </component> </class> </hibernate-mapping>
1.3 Contact.java class
There is nothing to pay attention to in this class file. You can add basic properties and you don’t need to configure corresponding mappings for this class, so its content is quite simple.
package com.src.hibernate; public class Contact { //email address private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } //Address private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } //Post code private String zipCode; public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } //Contact number private String contactTel; public String getContactTel() { return contactTel; } public void setContactTel(String contactTel) { this.contactTel = contactTel; } } 1.4 Generate results
After the above file configuration, the corresponding database table structure can be generated. The generated 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_increment, name varchar(255), email varchar(255), address varchar(255), zipCode varchar(255), contactTel varchar(255), primary key (id)) create table t_user (id integer not null auto_increment, name varchar(255), email varchar(255), address varchar(255), zipCode varchar(255), contactTel varchar(255), primary key (id))
The generated database table structure is as follows:
2. Data operation
The table structure obtained by combining mapping is a complete table, so the most original method can be implemented when writing and reading data. Here we also use the test methods used in the previous articles to write and read data, which are the use of save and load methods, see below for specific operations.
2.1 Insert data
Here, User is used as an example, and the write operation of Employee is the same as that of User. When writing data, two objects need to be created, one is the contact object and the other is the user object. When saving, only the user object needs to be saved, and the corresponding contact object will be saved together.
public void testSave1(){ //Declare the session object Session session=null; try{ //Get the session object session=HibernateUtils.getSession(); //Open the session session.beginTransaction(); //Create the connection object Contact userContact=new Contact(); userContact.setAddress("Beijing"); userContact.setContactTel("1243435"); userContact.setEmail("[email protected]"); userContact.setZipCode("zipCode"); //Create the user object User 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 (?, ?, ?, ?, ?)
Check the table structure as follows:
2.2 Read operation
Also using User as an example, Employee's operations are the same as the User object. The reading operation is quite simple, the code is as follows:
public void testLoad1(){ //Declare the session object Session session=null; try{ //Get the session object session=HibernateUtils.getSession(); //Open the session session.beginTransaction(); //Get the user object User user=(User)session.load(User.class, 1); System.out.println("User name: "+user.getName()); //Submit the session session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); session.getTransaction().rollback(); } finally{ HibernateUtils.closeSession(session); } }The corresponding results are generated as follows:
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=? Username: zhangsan
III. Comprehensive examples
Account:
public class Account implements Serializable{ private int id; private double money; 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 money) { this.money = money; } public Address getAddress() { return address; } public void setAddress(Address 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--><hibernate-mapping package="pojo"> <class name="Account" table="t_account" > <id name="id"> <column name="id"></column> <generator></generator> </id> <property name="money"> <column name="money"></column> </property> <component name="address"> <property name="code"> <column name="code"></column> </property> <property name="city"> <column name="city"></column> </property> <property name="province"> <column name="province"></column> </property> </component> </class></hibernate-mapping>