Hibernate component Mapping

Source: Internet
Author: User

1: Why to use component mappings

Answer: an important principle of establishing a relational data model is to minimize the number of database tables and the foreign key references between tables without causing data redundancy . Take employee information as an example, employee information in the employee's home address information, if the address information in a single table, and then establish the Employee Information table and the address information table between the foreign key reference relationship, when each query employee information, need to build two tables of connections. establishing a table connection is a time-consuming operation, in order to improve the performance of the database, you can combine the information of these two tables in an Employee Information table empinfo .

2: What is a component mapping : A component is a contained object that is stored in a table with its owner, meaning it is only a value type, not an entity . the difference between a value type and an entity is that the value type does not have an identifier, and of course persisting a value type does not require an identifier attribute

See the code:

Step One:

Create JavaBean

//Step One: Create Emphomeaddress and Empinfo Public classemphomeaddress {PrivateString Ehomestreet; PrivateString ehomecity; PrivateString ehomeprovince; PrivateString Ehomezipcode; Privateempinfo Empinfo;} Empinfo is created as follows: Public classEmpinfo {PrivateInteger Eid; PrivateString ename; Privateemphomeaddress ehome;}

Step Two: Create a configuration file EmpInfo.hbm.xml

<?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" >< Hibernate-mapping Package= "Cn.happy.component" > <className= "Empinfo" table= "Empinfo" > <id name= "Eid" column= "Eid" > <generatorclass= "Native" ></generator> </id> <property name= "ename" column= "ename" type= "string" ></pro perty> <component name= "Ehome"class= "Emphomeaddress" ><parent name= "Empinfo"/><property name= "Ehomestreet" column= "Ehomestreet" type= "string" ></property> <property name= "Ehomec ity "column=" ehomecity "type=" string "></property> <property name=" ehomeprovince "column=" Ehomeprovinc E "type=" string "></property> <property name=" Ehomezipcode "column=" Ehomezipcode "type=" string ">&lt ;/property> </component> </class> 

In the EmpInfo.hbm.xml file above, address is defined as a component of empinfo and used two times, respectively, corresponding to the two properties of Empinfo. According to the configuration file, Empinfo can get its components, the address will not get its owner, if you want address to get its owner, you can add a node in <component> <parent name= "Empinfo", adding the Empinfo attribute to the address class, and providing getter and setter methods to manipulate the Empinfo class using the Address.getempinfo ().

Step three: Test the code

 PackageTest;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;Importutil. Hibernateutil;Importentity. emphomeaddress;Importentity. Empinfo; Public classMyTest {@Test Public voidTest () {Session session=hibernateutil.currentsession (); Transaction TX=session.begintransaction (); Empinfo EMP=NewEmpinfo (); Emp.setename ("Jane Zhang"); //Create an Employee Address objectEmphomeaddress address=Newemphomeaddress (); Address.setehomecity (Beijing); Address.setehomeprovince (Beijing); Address.setehomestreet ("Five crossings"); Address.setehomezipcode ("100000");       Address.setempinfo (EMP);       Emp.setehome (address);       Session.save (EMP);       Tx.commit (); System.out.println ("ok==="); }}

Generated SQL statements and table structure:

Hibernate component 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.