Detailed description of the hibernate ing file xxx. hbm. xml in hibernate
I. Functions of Hibernate ing files:
The Hibernate ing file is a bridge between persistence of Hibernate and databases.
Ii. Main content of the Hibernate ing file:
(1). Definition of ing content:
The Hibernate ing file is composed The node defines the ing content and specifies the location of the corresponding JavaBean (or you may not use the package attribute to specify the corresponding JavaBean location in the node, specify the name attribute in the class node under the node)
(2 ).Association between databases and JavaBean:
Used in Hibernate ing files Under the node
Node specifies the association between database tables and JavaBean.
(The package name of JavaBean is specified in the package attribute of the parent node of the node)/full path (the package attribute is not used in the parent node of the node to specify the package name of the JavaBean)
(3 ).Primary Key ing:
In Nodes
The primary key corresponding to the node ing. This node must There is only one (because there is only one primary key) and must be placed in Before a node
(4 ).Common field ing:
In Nodes
Map common fields to a node. Multiple fields can be mapped to the node)
Iii. Primary Key ing method of heib.pdf:
In Nodes Node specifies the primary key generation method when Hibernate inserts data into the database
L assigned: the application itself assigns a value to the id. When set The application itself needs to assign values to the primary key id, Generated by external programs (set the value for the object's primary key before session. save (), without HibernateIs generally used when the primary key is a natural primary key. For example, when XH is the primary key, when you add a student information, you need the programmer to set the student id value, then you need to apply the id generator.
L native: assign values to IDS by databases. When set The database is responsible for assigning values to the primary key id, The most common is Int Type Auto-incrementing primary key. For example, if the id field of the table created in SQLServer is "identity" and the generator is configured, the programmer does not need to set the value for the primary key. It will automatically set the value.
L identity: uses the primary key generation mechanism provided by the database to generate unique identifiers for long/short/int columns, suchAuto-increment Primary Key Generation Mechanism in SQL Server and MySQL.
L hilo: The primary key generation mechanism implemented by the hi/lo algorithm requires additional database tables to save the historical state of the primary key generation.
L seqhilo: similar to hi/lo, the primary key generation mechanism implemented by the hi/lo algorithm only stores the historical state of the primary key in sequence, which is applicable to databases that support sequence, such as Oracle.
L increment: The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause a problem: if multiple instances access the same database, because each instance maintains the primary key status, different instances may generate the same primary key, this causes duplicate primary key exceptions. Therefore, if the same database has multiple instances to access, this method should be avoided.
L sequence: uses the sequence mechanism provided by the database to generate a primary key. It is used to generate a database (for example, Sequence of Oracle and DB2) with the sequence method ), used to generate a unique identifier for long/short/int columns, for example: Sequence name For example, Oracle sequence.
L uuid. hex: algorithms generated by Hibernate Based on 128-bit unique values, the hexadecimal value (expressed as a 32-Bit String After encoding) is generated based on the current device IP address, time, JVM start time, and internal auto-increment parameters. Even when multiple instances run concurrently, this algorithm maximizes the uniqueness of the generated id. Of course, the probability of repetition still exists in theory, but the probability is relatively small. Generally, using uuid. hex to generate a primary key will provide the best data insertion performance and Data Platform adaptability.
L uuid. string: similar to uuid. hex, it only encodes the generated primary key (the length is 16 bits ). Problems may occur in some databases.
L foreign: Use the field of the External table as the primary key. This primary key is generally applied to the relationship between the table and the table, and will be further explained in the following table mappings.
L select: the new primary key generation mechanism introduced by Hibernate 3, mainly for the transformation project of legacy systems.
Because common databases, such as SQLServer and MySQL, provide easy-to-use primary key generation mechanisms (such as auto-increase fields), you can use the primary key generation mechanism provided by the database., AdoptNative generator to configure the primary key generation method.
Iv. element structure and attributes of the ing File
1. root element: Each hbm. xml file has a unique root element, which contains optional attributes.
(1). package: Specifies the full package name of the persistent class corresponding to the current ing file (for example, package = entity. dao)
(2). schema: Specifies the schema name of the database table corresponding to the current ing file.
(3). catalog: Specifies the catalog name of the database table corresponding to the current ing file.
(4). default-cascade: sets the default cascade method (default value: none)
(5). default-access: Set the default attribute access method (default value: property)
(6). default-lazy: sets delayed loading for the ing classes and sets that do not specify delayed loading (the default value is true)
(7). auto-import: sets whether incomplete class names can be used in HQL in the current ing file (default value: true)
The Byte points of the hibernate-mapping node:
(1). class: Specify the corresponding persistent class name and database table name for the current ing file.
(2). subclass: Specify the subclass of the persistent class corresponding to the current ing file during the multi-state persistence operation.
(3). meta: Set the metadata attributes of a class or attribute.
(4). typedef: set a new Hibernate data type.
(5). joined-subclass: Specify the subclass of the current join.
(6). union-subclass: Specify the subclass of the current join
(7). query: defines an HQL query.
(8). SQL-query: defines an SQL query.
(9). filter-def: Specifies the filter.
2, Definition class : Child element of the root element, used to define the ing between a persistence class and a data table. The following lists some optional attributes contained in the element.
(1). name: Specify the corresponding persistent class name for the current ing File
(2). table: Specifies the database table name for the current ing file.
(3). schema: Set the schema name of the database table corresponding to the currently specified persistent class.
(4). catalog: Set the catalog name of the database table corresponding to the currently specified persistent class.
(5). lazy: Set whether to use delayed loading.
(6). batch-size: set the number of batch operation records (default value: 1)
(7). check: specify an SQL statement for condition check before Schema.
(8). where: Specifies the where condition for an additional SQL statement.
(9). rowid: Specifies whether ROWID is supported
(10). entity-name: the default object name is the class name.
(11). subselect: ing immutable read-only entities to database subqueries
(12). dynamic-update: Specifies whether the SQL statement used for update is dynamically generated. The default value is false.
(13). dynamic-insert: Specifies whether the SQL statement used for insert is dynamically generated. The default value is false.
(14 ). insert-before-update: sets whether to use the select statement to determine whether the object has been modified before Hibernate executes update. If the object value has not changed, the update statement will not be executed (the default value is false)
(15). abstract: used to identify abstract superclasses in the Union subclass (default value: false)
(16). emutable: indicates whether the instance of this class is variable. The default value is fals.
(17). proxy: Specify the delayed loading proxy class
(18). polymorphism: specifies that the default value of multi-state query is implicit.
(19). persister: Specifies a Persister class.
(20). discriminator-value: identifies a subclass. The default value is the class name.
(21). optimistic-lock: Specifies the optimistic Locking policy. The default value is vesion.
The Byte points of the class node:
(1). id: defines the primary key attributes of the persistent class corresponding to the current ing file and the information about the primary key fields in the data table.
(2). property: defines the attributes of the persistent class corresponding to the current ing file and related information of fields in the data table.
(3). SQL-insert: Use a custom SQL statement to execute the insert operation.
(4). SQL-delete: Use a custom SQL statement to perform the delete operation.
(5). SQL-update: Use a custom SQL statement to perform the update operation.
(6). subselect: defines a subquery.
(7). comment: Define table comments
(8). composite-id: the joint primary key corresponding to the persistent class and the database table
(9). Allow-to-one: defines the many-to-one association between objects.
(10). one-to-one: defines the one-to-one relationship between objects.
(11). any: defines the any ing type.
(12). map: map-type set ing
(13). set: set type set ing
(14). list: list type set ing
(15). array: array type set ing
(16). bag: Collection ing of the bag Type
(17). primitive-array: Set ing of the primitive-array type
(18). query: defines the HQL statement for loading objects.
(19). SQL-query: Define the SQL statement for loading objects
(20). synchronize: defines the synchronization resources required by the persistence class.
(21). query-list: ing the set returned by the query
(22). natural-id: declares a unique business primary key.
(23). join: map the attributes of a class to multiple tables.
(24). sub-class: Declares subclasses in the polymorphism ing.
(25). joined-subclass: concatenate child classes in the life-state ing.
(26). union-subclass: Declares the union subclass in the polymorphism ing.
(27). loader: a loader that defines persistent objects.
(28). filter: defines the filters used by Hibernate.
(29). component: define component ing
(30). dynamic-component: defines dynamic component ing
(31). properties: defines a logical group containing multiple attributes.
(32). cache: defines the cache policy.
(33). discriminator: defines an identifier.
(34). meta: Set the metadata attributes of a class or attribute.
(35). timestamp: specifies the time stamp data in the table.
(36). vesion: Specifies the data with version information contained in the table.
4, Define the primary key:
Hibernate uses OID (object identifier) to identify the uniqueness of an object. OID is the equivalent of a primary key in a relational database in the Java object model. during runtime, hibernate maintains the correspondence between Java objects and records in database tables based on the OID.
Attributes of the id node:
(1). name: Specifies the primary key name of the persistent class corresponding to the current ing.
(2). column: Specifies the primary key name in the database table corresponding to the current ing (the default value is the primary key/attribute name of the corresponding persistent class)
(3). type: Specifies the Data type of the primary key in the database table corresponding to the current ing.
(4). unsaved-value: determines whether the object is saved.
(5). daccess: the policy that Hibernate uses to access the primary key attribute (default value: property)
5. attributes of the generator node:
(1). class: Specifies the primary key generator.
(2). name: Specifies the primary key name of the persistent class corresponding to the current ing.
(3). column: Specifies the primary key name in the database table corresponding to the current ing (the default value is the primary key name in the corresponding persistent class)
(4). type: Specifies the Data type of the primary key in the database corresponding to the current ing.
(5). unique: Set whether the value of this field is unique (default value: false)
(6). not-null: Set whether the value of this field can be null (default value: false)
(7). update: Set whether the update operation contains the data of this field (the default value is true)
(8). insert: sets whether the insert operation contains the data of this field (the default value is true)
(9). formula: When you set a query operation, the value of this attribute is calculated using the specified SQL statement.
(10). access: the policy that Hibernate uses to access this attribute (default value: property)
(11). lazy: Set whether the field adopts a delayed loading policy (default value: false)
(12). optimistic-lock: Specifies whether optimistic Locking is required when this attribute is updated (the default value is true)
6. Properties of the property node:
The ing between persistence class attributes and database table fields includes the following attributes:
(1) name: attribute name of the persistence class, starting with a lowercase letter
(2) column: field name of the database table
(3) type: name of the Hibernate ing type
(4). formula: Set the attribute value of the persistent class corresponding to the current node to be obtained from the database by the specified SQL.
Note: The specified SQL statement must be enclosed by (). When using columns in the specified SQL statement, the alias of the table must be added. add column names, but if you want to use the current ing corresponding column in the specified SQL statement, you cannot use the table alias. add a column name for access, but directly access it.
For example: formula = (select tn. columnName from tableName tn where tn. columnName = attribute name in the current ing)
(5). unique: Set whether the value of this field is unique (default value: false)
(6). not-null: Set whether the value of this field can be null (default value: false)
(7 ). not-found: Set how to handle data that is referenced when the database field corresponding to the current node is foreign key does not exist (default value: exception, optional value: ignore: associate nonexistent applications with null)
(8). property-ref: Set the attribute name of the associated class. The default value of this attribute is the primary key of the associated class.
(9). entity-name: Object name of the associated class
(10 ). lazy: determines whether to adopt the delayed loading and loading policies (default value: proxy: Associate by proxy, optional value: true: this object uses delayed loading and is crawled when the variable is accessed for the first time. false: This correlated object does not use delayed loading)
(11). access: the policy that Hibernate uses to access this attribute (default value: property)
(12). optimistic-lock: Specifies whether optimistic Locking is required when this attribute is updated (the default value is true)
7. Attributes of one-to-one nodes:
(1). name: name of the ing Class Attribute
(2). class: name of the associated class
(3). formula: the vast majority of one-to-one links point to the primary key of the actual body. In some cases, it will point to one or more fields or an expression. At this time, an SQL formula can be used to represent
(4). cascade: sets the cascade type during cascade operations.
(5). constrained: Indicates whether there is a foreign key constraint between the table corresponding to the current class and the associated table. The default value is false.
(6). fetch: sets the data capturing policy. Optional values: join Outer join capturing and select sequence capturing.
(7). property-ref: Set the attribute name of the associated class. This attribute corresponds to the primary key of the associated class by default.
(8). access: the policy that Hibernate uses to access this attribute (default value: property)
(9 ). lazy: specifies whether to use the delayed loading and loading policies. The default value is proxy. The value of proxy is used for association. The optional value is true. This object uses delayed loading and is crawled when the variable is accessed for the first time. false. This correlated object does not use delay. load
(10). entity-name: Object name of the associated class
8. Sequence-to-one element:
(1). name: name of the ing Class Attribute
(2). class: name of the associated class
(3). formula: the vast majority of one-to-one links point to the primary key of the actual body. In some cases, it will point to one or more fields or an expression. At this time, an SQL formula can be used to represent
(4). column: link fields mapped from the intermediate joined table to the target joined table
(5). cascade: sets the cascade type during cascade operations.
(6). fetch: sets the data capturing policy. The default value is select sequence. The optional value is join Outer join capture.
(7 ). lazy: specifies whether to use the delayed loading and loading policies. The default value is proxy. The value of proxy is used for association. The optional value is true. This object uses delayed loading and is crawled when the variable is accessed for the first time. false. This correlated object does not use delay. load
(8). update: whether to include this field during the update operation
(9). insert: whether this field is included in the insert operation
(10). not-found: when the data referenced by the specified foreign key does not exist, how can we associate null with an exception when the default value is exception? optional value: ignore
9. Sequence-to-sequence element
10. set Element
(1). name: name of the ing Class Attribute
(2). table: The Associated target database table
(3). schema: schema name of the target database table
(4). catalog: The catalog name of the target database table
(5). subselect: defines a subquery.
(6). sort: Set the default value of the sorting type to unsorted. The optional value is natural sorting. comparatorClass implements the interface class as the Sorting Algorithm to avoid simultaneous use with order-.
(7). lazy: whether to use delayed Loading
(8). inverse: used to identify the passive party in the two-way Association. The default value is false.
(9). cascade: sets the cascade type during cascade operations.
(10). mutable: identifies whether the associated object can be changed. The default value is true.
(11). order-by: Set sorting rules
(12). where: adds filtering conditions.
(13). batch-size: the default value of the number of data read at a time during Delayed loading is 1.
(14). fetch: sets the optional values of the Data Capturing policy to join Outer join capturing and select sequence capturing.
11. list element
12. map Element