Hibernate Journey (Fri) Hibernate mapping--basic class mapping and object relational mapping

Source: Internet
Author: User
Tags generator



Recall that when we did not learn ssh, when we build the database table, the first is the database modeling e-r diagram, and then through the entity model to establish the relationship model, and then establish the corresponding table. There are three relationships between entities, one-to-many (or many-to-two), many-to-many. And now we're going to map the table based on the class, which can only be mapped by mapping files to the relationship between classes and classes. We learn UML modeling, there are five relationships between classes and classes, inheritance, implementation, association, dependency, aggregation/composition, and the relationship between entity classes in Hibernate, and we are already familiar with the corresponding code implementations for different relationships, so the entity class is a review of knowledge.

Hibernate is the essence of object-relational mapping (objectrelational Mapping), ORM Implementation of the object data is saved to the database, before we operate on the relational table, perform additions and deletions and other tasks, and now we no longer operate on the relational table, Instead, it operates directly on the object. ORM mapping files in hibernate are usually suffixed with. Hbm.xml. Using this mapping file is not only easy to read, but it can be modified manually, and some tools can be used to generate the mapping document. The mappings in hibernate are described below.

Hibernate mapping classification, as shown in the following figure.

1 Basic class Mappings

Create the corresponding table from the entity class, and this simple relationship is the Hibernate basic mapping.

The User1 entity class code is as follows:

User entity. Public
ClassUser1 {
   //user number.
   private String ID;
  
   Name.
   private String name;
  
   Password.
   private String password;
  
   The creation date.
   private Date createtime;
  
   Failure time.
    private Date expiretime;
 
   Public String getId () {
      return ID;
   }
 
Publicvoid setId (String ID) {
//    this.id= ID;
} public
 
   String getName () {
      return name;
   }
 
   public void SetName (String name) {
      this.name = name;
   }
 
   Public String GetPassword () {
      return password;
   }
 
   public void SetPassword (Stringpassword) {
      this.password = password;
   }
 
   Public Date Getcreatetime () {
      return createtime;
   }
 
   public void Setcreatetime (datecreatetime) {
      this.createtime = createtime;
   }
 
   Public Date Getexpiretime () {
      return expiretime;
   }
 
   public void Setexpiretime (dateexpiretime) {
      this.expiretime = expiretime;
   }
 }

The User1.hbm.xml mapping file looks like this:

 

Converts a User1 object to a table T_user1 in a relational database through a User1.hbm.xml mapping file.

The result of the conversion is as follows:


2 Object Relational Mappings
2.1 Multiple-to-one correlation mapping (unidirectional)

For example, the relationship between users and groups is a many-to-one relationship, and multiple users correspond to one group.


Maps an entity to a table and maps the corresponding entity to a table. The corresponding attributes are mapped into table fields.

A many-to-one association map maintains the associated field on a multiple side, and in our case, the relationship field is maintained at the user's end.

User.hbm.xml file.

 


Group.hbm.xml file.

 


The code we look at here looks at the *.HBM.MLX code, because for the association between classes, when implemented, a class as a private member of another class, this is learning UML modeling when we all understand, here is mainly to see the Orm M, that is, the *.hbm.xml file.

2.2 One-to-one correlation mappings

One-to-one correlation mapping in real life is more common, such as the relationship between people and home address, through the object can find his home address related content. 2.2.1 A-to-one mapping (unidirectional primary Key Association)


One-way primary key association, by their primary key is equal, from the person can see Idcard, that is, the t_idcard in the primary key is taken as the primary key T_pseron.

In the XML file:

<class name= "Com.bjpowernode.hibernate.Person" table= "T_person" >
      <id name= "id" >
      <!-- With the foreign generation strategy, foreign gets the identity of the associated object--
         <generator class= "foreign" >
         <!--property refers to the associated object. --
            <param name= "property" >idCard</param>
         </generator>
      </id>
      <property name= "name"/>
      <!--one-to-one correlation mapping, primary key association.
      -<!--the
      one-to-one tag indicates how hibernate loads its associated objects, which are loaded by default based on the primary key.
      That is, you get the relationship field value, and the associated object is loaded based on the primary key of the peer.
      Constrained= "true", which indicates whether the current primary key (the person's primary key) or a foreign key.
      Referring to the primary key of the peer (the primary key of the Idcard), the FOREIGN KEY constraint statement is generated.
      --
      <one-to-one name= "Idcard" constrained= "true"/>
   </class>

 

A one-on relationship is defined by the one-to-one element.


2.2.2 One-to-one mapping (bidirectional primary Key Association)

A one-to-one, two-way primary Key association is the same as the single-way primary key association, which is a single-way primary key association, where Idcard can be seen on the person side, and Idcard cannot see the person side. The two-way association is also able to see the person at the Idcard end, that is, not only add <one-to-one> tags in the Person.hbm.xml, but also add <one-to-one> in the IdCard.hbm.xml file. Label. The code is shown below.

 



2.2.3 Single-to-one mapping (unidirectional unique Foreign Key association)

One-way unique foreign Key association, which is a special case of many-to-one association, restricts the one end to the other, which is a unique foreign key association. Same as many to one, at one end of the other side and adopt the <many-to-one> tag, by unique= "true", so as to limit the number of the end of a.

First, the code.

IdCard.hbm.xml

 

Person.hbm.xml

 


The figure is as follows:


With a foreign key field idcard on the T_pserson side, limiting the uniqueness of Idcard is one-to-single foreign Key association.


2.2.4 One-to-one mapping (bidirectional unique Foreign Key Association)

Single-to-one foreign key unidirectional association we have learned that the two-way reverse is in the absence of the end of the add on it.

<one-to-one> tags are used in our IdCard.hbm.xml.

 

And Person.hbm.xml is the same as one-to-single foreign key unidirectional association.

 <class name= "Com.bjpowernode.hibernate.Person" table= "T_person" > <id name= "id" > <!--adopt foreign generation strategy, foreign will get the identity--> of the associated object; 

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.