Hibernate from entry to Mastery (iv) Basic mapping

Source: Internet
Author: User

The concept of mapping

In the last post hibernate from getting started to proficient (c) Hibernate configuration file We have explained the two configuration files in Hibernate, where the main difference between the two profiles is that XML can configure mappings. The mapping referred to here is the Object relational mapping (relational Mapping). The goal of ORM is to save the object data to the database and to read the database data into the object so that the developer can turn the operations of the database data into operations on those objects.

Basic mapping

After understanding the meaning and concept of mapping, let's explain the classification of mappings. From the concept of mapping, a single database table can be mapped into an object, aside from the hibernate, this idea is common in many project development, that is, based on the table structure to create the corresponding entity class, this simple mapping relationship in the hibernate map is called the basic mapping.

Basic Mapping instance

The table structure as shown in the following illustration:

After the conversion, you can create the appropriate object, the following code:

public class user{  
    private String ID;    
    private String name;  
          
    Public String GetId () {return  
        ID;  
    }  
    public void SetId (String id) {  
        this.id = ID;  
    }  
    Public String GetName () {return  
        name;  
    }  
    public void SetName (String name) {  
        this.name = name;  
    }  
}

For hibernate, just the above is not enough, but also need an XML-formatted mapping file User.hbm.xml, 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 ">  

Annotation Configuration Basic Mapping instance

In addition to the above XML configuration mappings, you can configure the mappings by adding annotations to the class file as follows:

@Entity
@Table (name= "T_user") Public  
class user{  
    @Id
    private String Id;    
    @GeneratedValue
    private String name;  
          
    Public String GetId () {return  
        ID;  
    }  
    public void SetId (String id) {  
        this.id = ID;  
    }  
    Public String GetName () {return  
        name;  
    }  
    public void SetName (String name) {  
        this.name = name;  
    }  
}

To this, the basic concept of mapping and simple and practical, we have been explained, for mapping configuration of some of the details of the problem, such as: @Entity, @Table use instructions, and so on, we recommend that you find a note and XML configuration related to the documentation to learn, here will not be explained to you. Next time we'll explain some of the slightly more complex mappings.

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.