Object-Relational mapping (Object/relation Mapping, or ORM), which arises from the development of object-oriented software development methods, is a technique to solve the phenomenon of non-matching between object-oriented and relational database. Essentially, it's converting data from one form to another. Hibernate, as the representative of an ORM framework, is implemented by the basic mapping of hibernate in implementing ORM.
Some of the common hibernate mappings: Basic mappings and relational mappings. This blog mainly introduces the basic mapping of hibernate.
The main files that Hibernate uses when implementing ORM functions are:
1, Mapping Class (*. Java): It is the structure that describes the database table, the fields in the table are described as attributes in the class, and in the future it is possible to map the records in the table into objects of that class.
2. mapping file (*.hbm.xml): It is the relationship between the specified database table and the mapping class, including mapping class and database table correspondence, table field and Class property type corresponding relationship, and table field and class property name corresponding relationship, etc.
3. Hibernate core configuration file (*.properties/*.cfg.xml): It specifies some of hibernate's core configurations, including connection information needed to connect to the database, such as which database to connect to, the user name to log in to the database, Login passwords, connection strings, and so on. The address information for the mapping file is also placed here.
Basic mapping:
Java classes are mapped into tables:
<strong><span style= "FONT-SIZE:18PX;" >package Com.bjpowernode.hibernate;import Java.util.date;public class User {private string id;private string name; private string password;private date createtime;private date expiretime;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;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public Date Getcreatetime () {return createtime;} public void Setcreatetime (Date createtime) {this.createtime = Createtime;} Public Date Getexpiretime () {return expiretime;} public void Setexpiretime (Date expiretime) {this.expiretime = Expiretime;}} </span></strong>
Design principles for entity classes:
Implementing a default constructor with no parameters
Provide an identity
We recommend that you do not use FIANL to modify entity classes
We recommend generating get and set methods for entity classes
mapping files: User.hbm.xml
<strong><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
in the Hibernate mapping file, use the class The element represents a mapping between a class and a table in a database. If you want to modify the table name, you can use the Tables property. The table property specifies the corresponding database table name to map, and if omitted, name as the table name. The Name property specifies the Java fully qualified name of the persisted class (or interface). Length property: Indicates the character size of the field, which can be designed in a database or written in a mapping file.
Columns: You can modify the column names of the columns for the specified mappings. If omitted, the name is named as the column name.
Hibernate core configuration file:<strong><span style= "FONT-SIZE:18PX;" ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
Hibernate's core configuration file, you can configure some of the actual information for the SQL statement: whether the SQL statement is displayed in the console, Hibernate.show_sql is set to true.
Hibernate basic mapping, mainly can be through the configuration file, but also through the form of annotations, in the following blog, the small series will take you to learn if you use annotations to do the basic mapping.
Hibernate advanced Hibernate Basic Mapping