Proficient in hibernate--mapping composition relationship

Source: Internet
Author: User

There are different starting points for establishing domain model and relational data model. The domain model is composed of program code, which provides code reusability and simplifies programming by refining the granularity of the persisted class. The relational data model consists of relational data. In the case of data redundancy, it is necessary to split the coarse-grained table into several fine-grained tables with foreign key reference relations, thus saving the table storage space, on the other hand, without data redundancy, we should minimize the number of tables and simplify the referential relationship between tables so as to improve the database access speed.
Because of the different principles of establishing a domain model and relational data, the number of persisted classes tends to be more than the number of tables in the database, and the properties of the persisted class do not correspond to the table field one by one. The HomeAddress property of the customer class and the Comaddress property are all corresponding to multiple fields in the Customer table
For example:

//Address public class  @MapperedSuperclass Span class= "Hljs-comment" >//if it is an annotation, label this  public      Class  address{private  String Province; private     String City; private     String Street; private     String ZipCode; private     Customer customer; private     Customer getcustomer  () {return  customer; } private  void      Setcustomer  (Customer customer) {this . Customer = customer; }}
// 客户信息类@Table(name="CUSTOMER")publicclass Customer extends Address{    private String id;    private String name;    private Address homeAddress;    private Address comAddresss;}

When you create an object-relational mapping file, you cannot use the property element to map the HomeAddress attribute, but to use the component element, as follows:

<componentname="HomeAddress" class="com. Address "> <parentname="Customer"/> < Property name="Street"Type="string"column="Home_address"/> < Property name="City"Type="string"column="Home_city"/> < Property name="province"Type="string"column="Home_province"/> < Property name="ZipCode"Type="string"column="Home_zipcode"/></component>

The component element means that homeaddress is an integral part of the customer class, which is called a component in Hibernate and has the following two attributes:
1.name: Sets the property name of the persisted class being mapped, here is the HomeAddress property of the Customer class
2.class: Sets the type of the HomeAddress property, which indicates that the HomeAddress property is the address type
The component element also contains a parent element and a property element, and the parent element specifies the whole class to which the address class belongs, which is set to customer, in which case a customer attribute should be defined in the address class. and provides a get, set method.
The property child element of the component element is used to configure the properties of the component class and the mappings of the fields in the table.

1. Type of distinction (value) and entities (entity)
The above address class does not have an OID, which is an important feature of the Hibernate component. Because the address class does not have an OID, you cannot save, update, delete, or load an address individually through the session, or you will throw a mappingexception.
The important difference between a value type and an entity type is that the former has no OID and cannot be persisted independently, and his life cycle relies on the life cycle of the object of the owning persisted class, which is a value type, and the entity type has an OID, which can be persisted independently.

2. Mapping Composite Composition Relationship
For example, Cpubox is a component of computer, and Cpubox itself contains a Graphicscard component, in addition, Cpubox is associated with vender many-to-one.
The configuration is as follows:

class name="Com.computer"table="Computer"> <ID name="id"Type="Long"column="ID"> <generatorclass="Increment"/> </ID> < Property name="Type"Type="string"> <columnname="Compert_type" length=" the"/> </ Property> <componentname="Cpubox" class="com. Cpubox "> <parentname="Computer"/> < Property name="Type"Type="string"> <columnname="Cpubox_type" length=" the"/> </ Property> <componentname="Graphicscard" class="com. Graphicscard "> <parentname="Cpubox"/> < Property name="Type"Type="string"> <columnname="Graphicscard_type" length=" the"/> </ Property> </component> <many- to-onename="Vendor"column="cpubox_vendor_id" class="com. Vendor "  not-null="true"/> </component> </class>

Proficient in hibernate--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.