before we understand the hibernate framework, we should first understand the concepts of object-relational mapping (ORM) and relational database.
Object-Relational mapping (ORM) is primarily a mapping of the implementation of program objects to relational database data. Why do you say that?
A relational database is a mainstream data storage system that permanently stores data in an enterprise-wide application environment. object and relational data are two representations of business entities, and business entities behave as objects in memory and behave as relational data in the database. There are associations and inheritance relationships between objects in memory, and in a database, relational data cannot directly express many-to-many associations and inheritance relationships. looking back at our previously developed projects, we are doing some mapping in the process of manipulating the database. For example, there is a user object, what we need to do is to save this object to which table in the database, the object's properties correspond to which fields of the database table, and to do this requires mapping. In the past, these mappings were done by ourselves. Now with the hibernate framework, these relationships can be maintained.
Hibernate provides several ways of mapping:
Hibernate Basic components:
- Hibernate Core configuration file: Hibernate.cfg.xml file, complete the basic configuration
- Entity class (*.java): A mapping class that describes object names and attributes, table names and fields in the corresponding table
- Mapping file (*.hbm.xml): Specifying the relationship between a mapping class and a database table
Basic mapping
1.hibernate Core configuration file
<!--Declare the start of the Hibernate profile--
2. Entity ClassesPackage Com.blackfox.hibernate;import Java.util.date;public class User {private string Id;private string name;private String Password;public string GetId () {return 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;}}
design principles for entity classes:
- Implementing a default constructor with no parameters
- Provide an identity
- It is not recommended to use fianl Modifying entity classes
- recommended generation for entity classes Getter and the Setter Method
3. mapping Filesordinary attributes in the entity class (excluding collections, customizations, and arrays) are mapped into table fields, using<property>Label Mapping
<!--You can declare the package name-->
Note: If the name of the entity class or the name of the attribute in the entity class and the database keyword are duplicated, the problem can be considered to be renamed with the table property and column .
Hibernate is a persistent layer, a bridge between our programs and our database, and lets us deal with the database in an object-oriented way. Benefit: Completely encapsulates the JDBC hidden details, resulting in portability. Cons: The package is too thorough, inflexible, and there are problems with the update operation of the large data volume.
Hibernate's basic mapping is just the tip of the iceberg, and here are some of the associated mappings for hibernate.
Hibernate mapping Parsing (i)--basic mapping