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.