Detailed Hibernate mapping file

The mapping between Hibernate's persistent classes and relational databases is usually defined using an XML document. This document establishes a one-to-one mapping between persistent classes and database tables through the configuration of a series of XML elements. This means that the mapping document is created according to the definition of the persistent class, not the definition of the table .

1. Root element: <hibernate-mapping>, each hbm.xml file has a unique root element that contains some optional attributes

1) package : Specify a package prefix. If the fully qualified class name is not specified in the mapping document, use this as the package name, such as

<hibernate-mapping package="com.demo.hibernate.beans">

<class name="User" ...>

</hibernate-mapping>

<hibernate-mapping>

<class name="com.demo.hibernate.beans.User" ...>

</hibernate-mapping>

2) schema: the name of the database schema

3) catalog: the name of the database catalog

4) default-cascade : the default cascade style, the default is none

5) default-access: The strategy used by Hibernate to access properties

6) default-lazy : specifies the Java properties and collection classes that do not explicitly indicate the lazy property, what default loading style Hibernate will take, the default is true

7) auto-import : Specifies whether we can use non-fully qualified class names in the query language, the default is true, if there are two persistent classes with the same name in the project, it is best to map the corresponding classes of these two classes The file is configured to false

2. <class> defines a class: a child element of the root element, used to define the mapping relationship between a persistent class and a data table. The following are some optional attributes contained in this element

1) name : Java fully qualified name of the persistent class (or interface), if this property does not exist, Hibernate will assume this is a non-POJO entity mapping

2) table : corresponds to the database table name

3) discriminator-value: The default is the same as the class name, a value used to distinguish different subclasses, used in polymorphic behavior

4) mutable: indicates that the instance of the class is mutable or immutable

5) schema: Override the schema name specified in the root element <hibernate-mapping>

6) catalog: Override the catalog name specified in the root element <hibernate-mapping>

7) proxy: specify an interface to be used as a proxy during lazy loading

8) dynamic-update : The SQL specified for UPDATE will be dynamically generated at runtime and only those fields that have changed will be updated

9) dynamic-insert : The SQL specified for INSERT will be dynamically generated at execution time and only include those non-null fields

10) select-before-update : Specifies that HIbernate will not perform SQL UPDATE operations unless it is determined that the object has been modified (if the value is true). In certain cases (actually, it only takes effect in update() executed when a transient object is associated with a new Session), this means that Hibernate will perform an additional SQL SELECT operation before the UPDATE to decide whether it should be executed or not. UPDATE

11) polymorphism: polymorphism, defining whether it is an implicit or explicit polymorphic query

12) where : Specify an additional SQLWHERE condition, which will be added when grabbing objects of this class

13) persister: Specify a custom ClassPersister

14) batch-size: Specify a 'batch size' (the number of batches) used when grabbing instances based on identifiers

15) optimistic-lock : optimistic locking, which determines the optimistic locking strategy

16) lazy: by setting lazy="false", all lazy loading (Lazy fetching) functions will not be activated (disabled)

17)entity-name

18) check: This is an SQL expression used to add a multi-row constraint check to the automatically generated schema

19)rowid

20)subselect

21) abstract: used to identify abstract superclasses in the inheritance structure (hierarchies) of <union-subclass>

3. <id> defines the primary key: Hibernate uses OID (object identifier) ​​to identify the uniqueness of the object. OID is the equivalent of the primary key in the relational database in the Java object model. At runtime, Hibernate maintains Java objects and databases according to the OID. Correspondence of records in the table

1) name : the name of the identity attribute of the persistent class

2) type : the name that identifies the Hibernate type

3) column : the name of the primary key of the database table

4) unsaved-value : used to mark that the instance has just been created and has not been saved. Can be used to distinguish the state of an object

5) access: The strategy used by Hibernate to access property values

If the table uses a federated primary key, then you can map multiple properties of the class to identifier properties. The <composite-id> element accepts a <key-property> attribute map and a <key-many-to-one> attribute map as child elements:

The following defines two fields as the union primary key:

<composite-id>

<key-property name="username" />

<key-property name="password" />

</composite-id>

 

Fourth, <generator> sets the primary key generation method

The function of this element is to specify the generator of the primary key, and specify the class corresponding to the generator through a class attribute. (usually used in conjunction with the <id> element)

<id name="id" column="ID" type="integer">

<generator class="native" />-- native is one of the Hibernate primary key generator implementation algorithms. Hibernate uses one of identity, hilo, and sequence as the primary key generation method based on the underlying database.

</id>

Built-in generators provided by Hibernate:

1) assigned algorithm

2) Hilo algorithm

3) seqhilo algorithm

4) Increment algorithm

5) identity algorithm

6) sequence algorithm

7) native algorithm

8) uuid.hex algorithm

9) uuid.string algorithm

10) Foregin algorithm

11) select algorithm

 

Five, <property> defines properties

The mapping between the properties of the persistent class and the database table fields, including the following properties:

1) name : The attribute name of the persistent class, starting with a lowercase letter

2) column : the field name of the database table

3) type : the name of the Hibernate mapping type

4) update: Indicates whether the mapped field is included in the SQL statement used for UPDATE, the default is true

5) insert: Indicates whether the SQL statement used for INSERT contains the mapped field or not, the default is true

6) formula: an SQL expression that defines the value of this computed property

7) access: The strategy used by Hibernate to access property values

8) lazy: Specifies whether this property is delayed when the instance variable is accessed for the first time, the default is false

9) unique: use DDL to add a unique constraint to the field, in addition, this can also be used as the target attribute of property-ref

10) not-null: Use DDL to add a nullable constraint to the field

11) optimistic-lock: Specifies whether this property needs to obtain optimistic locking when updating (in other words, it determines whether the value of the version version increases when dirty data occurs in this property)

The access property is used to let you control how Hibernate accesses properties at runtime. By default, Hibernate will use the property's get/set method pair. If you specify access="field", Hibernate will ignore the get/set method pair and use reflection to access member variables directly.

The formula property is a particularly powerful feature. These properties should be defined as read-only , and property values ​​are computed at load time. Use an SQL expression to generate the result of the calculation, which will be translated into a SELECT subquery of an SQL query when the instance is reproduced. Such as:

<property name="totalPrice" formula="(SELECT SUM(*) FROM user)" />

Reprinted from: https://www.cnblogs.com/ani-deng/p/4772559.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768740&siteId=291194637