Hibernate's mapping composition relationship

Source: Internet
Author: User

Establishing a domain model (the Java object Model) and a relational data Model (database table model) have different starting points:
Domain model: consists of program code that improves the reusability of code by refining the granularity of the persisted class (that is, by planning the same attribute as a class) to simplify programming
  Relational model: In the absence of data redundancy, you should minimize the number of tables and simplify the referential relationships between tables to improve data access speed

Hibernate divides the properties of a persisted class into two types:
Value type: No OID, cannot be persisted independently, life cycle depends on the life cycle of the object of the owning persisted class
Entity type: Has OID, can be persisted independently, has independent life cycle obviously cannot map entity class attribute directly with property
Hibernate uses the <component> element to map the constituent relationships (that is, it is possible that multiple persisted classes form a table), the element table name pay property is a component of the Worker class, which is called a component in Hibernate

Example:
Create a new worker class:

 Packagecom.atguigu.hibernate.entities; Public classWorker {PrivateInteger ID; PrivateString name; PrivatePay pay ;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicPay Getpay () {returnPay ; }     Public voidSetPay (Pay pay) { This. Pay =Pay ; }            }

The worker class also contains a pay class:

 Packagecom.atguigu.hibernate.entities; Public classPay {Private intMonthlypay; Private intYearpay; Private intVocationwithpay; Privateworker worker;  PublicWorker Getworker () {returnworker; }     Public voidSetworker (worker worker) { This. Worker =worker; }     Public intGetmonthlypay () {returnMonthlypay; }     Public voidSetmonthlypay (intMonthlypay) {         This. Monthlypay =Monthlypay; }     Public intGetyearpay () {returnYearpay; }     Public voidSetyearpay (intYearpay) {         This. Yearpay =Yearpay; }     Public intGetvocationwithpay () {returnVocationwithpay; }     Public voidSetvocationwithpay (intVocationwithpay) {         This. Vocationwithpay =Vocationwithpay; }            }

The question is, how to put these two classes at the same time in a table???

Can be configured with a. hbm.xml file

We can use the <component> tag to specify the classes contained in a class

  

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2014-1-2 16:14:33 by Hibernate Tools 3.4.0.CR1 -<hibernate-mapping Package= "Com.atguigu.hibernate.entities">    <classname= "Worker"Table= "WORKER">        <IDname= "id"type= "Java.lang.Integer">            <columnname= "ID" />            <Generatorclass= "Native" />        </ID>        < Propertyname= "Name"type= "Java.lang.String">            <columnname= "NAME" />        </ Property>                <!--Mapping Composition Relationships -        <Componentname= "Pay"class= "Pay">            <Parentname= "worker"/>            <!--Specifies the properties of the component that makes up the relationship -            < Propertyname= "Monthlypay"column= "Monthly_pay"></ Property>            < Propertyname= "Yearpay"column= "Year_pay"></ Property>            < Propertyname= "Vocationwithpay"column= "Vocation_with_pay"></ Property>        </Component>            </class></hibernate-mapping>

This allows you to designate multiple persisted classes as a single table,

    

Hibernate's mapping composition relationship

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.