Hibernate is an open-source framework for a thorough ORM (Object relational Mapping, objects relational mapping).
Let's take a look at the top-level view of the Hibernate architecture given by the official documentation:
Where the po=pojo+ mapping file
Depending on the architecture view, you can see that the entire project implemented with the Hibernate framework includes the entire important configuration file:
Hibernate configuration file: Hibernate basic configuration, hibernate can be friendly to the base of the interaction with the DB;
Development when placed in the SRC directory, named: Hibernate.cfg.xml (hibernate.properties)
Hibernate mapping File: Implements the mapping configuration of Pojo and DB tables;
In order to maintain the convenience of the general place and the corresponding Pojo in the same directory, named POJOName.hbm.xml. Although you can configure multiple Pojo mappings to a database table in a mapping file, it is recommended that you only configure one Pojo mapping with the database tables in a mapping file.
The mappings between Hibernate's persistence classes and relational databases are usually defined with an XML document. The document establishes a one by one mapping between a persisted class and a database table through the configuration of a series of XML elements. This means that the mapping document is created according to the definition of the persisted class, not the table definition.
The root element:
1) Package: Specify a packet prefix, if you do not specify a fully qualified class name in the mapping document, use this as the package name, as
<class name= "User" ...>
<class name= "Com.demo.hibernate.beans.User" ...>
2) Schema: Name of the database schema
3) Catalog: Name of the database catalog
4) Default-cascade: Default cascading style, default to None
5) Default-access:hibernate the policy used to access attributes
6) Default-lazy: Specifies the Java attribute and collection class for which the lazy attribute is not explicitly specified, and what default load style will hibernate take, which defaults to True
7) Auto-import: Specifies whether we can use a non-fully qualified class name in the query language, by default, true if there are two persisted classes with the same name in the project, it is best to configure false in the corresponding mapping file for these two classes
b <class> Definition class: A child element of the root element that defines a mapping relationship between a persisted class and a data table, as follows, some optional attributes that the element contains
1) Name: the Java fully qualified name of the persisted class (or interface), and if this attribute does not exist, Hibernate assumes that this is a non-pojo entity mapping
2) Table: corresponding database table name
3) Discriminator-value: The default is the same as the class name, a value that distinguishes between different subclasses, used when polymorphic behavior
4) mutable: Indicates that instances of this class are mutable or immutable
5) Schema: Overrides the schema name specified in the root element
6) Catalog: covers the root element
7) Proxy: Specify an interface to use as a proxy on deferred load
8) Dynamic-update: Specifies that SQL for update will be dynamically generated at run time, and only those fields that have changed have been updated
9) Dynamic-insert: Specifies that SQL for insert will be dynamically generated at execution time and contain only those non-null value fields
Select-before-update: Specifies that hibernate does not perform a SQL update operation unless it is determined that the object is actually modified (if the value is true). On a particular occasion (in fact, it only takes effect in the update () that executes when an instantaneous object is associated to a new session), which means that hibernate performs an additional SQL select operation before the update to determine whether the update should be performed
One) polymorphism: polymorphic, defining a polymorphic query with implicit or explicit patterns
Where: Specifies an additional sqlwhere condition that will be added when fetching objects of this class
Persister: Specify a custom Classpersister
Batch-size: Specify a ' batch size ' (number of batch fetches) to use when fetching instances by identifier (identifier)
Optimistic-lock: Optimistic locking strategy that determines optimistic locking
() Lazy: All deferred load (lazy fetching) functions will not be activated by setting lazy= "false" (disabled)
Entity-name)
Check: This is an SQL expression that adds a multiline (multi-row) constraint check to the automatically generated schema
ROWID)
Subselect)
Abstract: Used in the inheritance structure of <union-subclass> (hierarchies) to identify a superclass
Iii. <id> defining a PRIMARY key: Hibernate uses the OID (object identifier) to identify the uniqueness of the object, and the OID is the equivalent of the primary key in the relational database in the Java object model, at run time, Hibernate maintains the correspondence between Java objects and records in database tables based on OIDs
1) Name: Name of the identity property of the persisted class
2) Type: Name that identifies the hibernate type
3) Column: The name of the primary key of the database table
4) Unsaved-value: Used to flag that the instance was just created and has not been saved. can be used to distinguish the state of an object
5) Access:hibernate the policy used to access the property value
If the table uses a federated primary key, then you can map multiple properties of the class to the identifier property. The <composite-id> element accepts <key-property> attribute mappings and <key-many-to-one> attribute mappings as child elements:
The following defines two fields as the Federated primary Key:
<composite-id>
<key-property name= "username"/>
<key-property name= "Password"/>
</composite-id>
Iv. <generator> Setting the primary key generation mode
The function of this element is to specify the generator for the primary key, specifying the class for the generator with a class attribute. (usually used in conjunction with <id> elements)
<id name= "id" column= "id" type= "integer" >
<generator class= "native"/>--native is one of the implementation algorithms of Hibernate primary key generator, which is determined by hibernate according to the underlying database, using identity, Hilo, Sequence one of them as the primary key generation method.
</id>
Hibernate provides built-in generators:
1) Assigned algorithm
2) Hilo algorithm
3) Seqhilo algorithm
4) Increment algorithm
5) Identity algorithm (recommended)
6) Sequence algorithm
7) Native algorithm
8) Uuid.hex algorithm
9) uuid.string algorithm
TEN) Foregin algorithm
One) Select algorithm
V. <property> define Attributes
The mappings between the properties of the persisted class and the database table fields include the following properties:
1) Name: The property name of the persisted class, starting with a lowercase letter
2) Column: The field name of the database table
3) Name of the type:hibernate mapping type
4) Update: Indicates whether the mapped field is included in the SQL statement used for update, and the default is True
5) Insert: Indicates whether the SQL statement used for the insert contains this mapped field, and the default is True
6) formula: An SQL expression that defines the value of this computed property
7) Access:hibernate the policy used to access the property value
8)Lazy: Specifies whether this property is delayed fetching when the instance variable is first accessed, and false (related to the relationship mapping problem later)
9) Unique: Use DDL to add a unique constraint to the field, and this can also be used as the target property of Property-ref
Not-null: Use DDL to add a nullable constraint to this field
Optimistic-lock: Specifies whether this property needs to be optimistic when it is updated (in other words, it determines whether the value of version versions grows when dirty data is occurring for this property)
The Access property is used to let you control how hibernate accesses properties at run time. By default, Hibernate uses the property's Get/set method pair. If you indicate access= "field", Hibernate ignores the Get/set method pair and directly uses reflection to access the member variable.
The formula attribute is a particularly powerful feature. These properties should be defined as read-only, and the property values are evaluated at load time for generation. A SQL expression is used to generate the result of the calculation, which is translated into a SELECT subquery statement for a SQL query when this instance is reproduced. Such as:
<property name= "Totalprice" formula= "(select SUM (*) from user)"/>
Such as:
<!--map Derived properties-
<property name= "desc" formula= "(Select Guang (author,": ", title) from NEWS where news.id=id></property>
Hibernate mapping file Detailed (News***.hbm.xml) a