Xxx.hbm.xml Detailed summary of mapping files in hibernate

Source: Internet
Author: User

Transfer from http://blog.csdn.net/a9529lty/article/details/6454924

The role of the Hibernate mapping file:

Hibernate mapping file is a bridge for hibernate to persist with the database

Two, Hibernate mapping file main content:

(1). Definition of the mapping content:

The hibernate mapping file defines the mapping content by


(2). database and JavaBean associations :

Hibernate mapping file is used in the

<class name= "JavaBean name" table= "table name in corresponding database" > node specifies the association of the database table and JavaBean.

(used by the parent node of the node when the package name of JavaBean is specified with the packages attribute)/full path (the parent node of the node does not use the package attribute to specify JavaBean for the packages name)


(3). primary Key Mappings :

Under the <class > node

<id name= "The primary key in the JavaBean of the primary key in the database," column= "the primary key name in the database," type= "string" > Node mapping, which must have only one (because the primary key is only one) and must be placed in the <property ...> node Front

(4). normal field mapping :

Under the <class ...> node

<property name= "The field name in the JavaBean property name" column= "database in the database is" type= "string"/> node maps the normal field, which can have multiple (one field is mapped with one node)

Third, heibernate the primary key mapping mode:

Use <generator class= "map mode"/> node under <id ...> node to specify how the primary key is generated when hibernate inserts data into the database

L Assigned: The application itself assigns a value to the ID. When setting <generator class= "Assigned"/>, the application itself is responsible for assigning the primary key ID, which is generated by the external program (before Session.save () sets the value of the object's primary key), No hibernate participation is required , typically when the primary key is a natural primary key. For example, when you add a student information to a primary key, you need the programmer to set the value of the XH, and you need to apply the ID generator.

L Native: The ID value is assigned by the database. When setting <generator class= "native"/>, the database is responsible for the assignment of the primary key ID, most commonly the self-increment primary key of the int type. For example, in SQL Server, the ID field of the table is established and the generator is configured so that the programmer does not set a value for the primary key and it is set automatically.

L Identity: Using the primary key generation mechanism provided by the database, generate a unique identifier for the Long/short/int type column, such as SQL Server, MySQL in the self-enhanced primary key generation mechanism.

Hilo: The primary key generation mechanism implemented by the HI/LO algorithm requires additional database tables to hold the primary key generation history state.

L Seqhilo: Similar to Hi/lo, the primary key generation mechanism implemented by the HI/LO algorithm is only the primary key historical state saved in sequence, which is applicable to the database that supports sequence, such as Oracle.

L Increment: The primary key is incremented by numeric order. The implementation mechanism of this method is to maintain a variable in the current application instance to hold the current maximum value, and then add 1 to the primary key each time a primary key is generated. The possible problem with this approach is that if there are multiple instances accessing the same database, the different instances may generate the same primary key as each instance maintains its primary key state, causing the primary key to repeat the exception. Therefore, if you have multiple instance accesses to the same database, you should avoid using

L Sequence: Use the sequence mechanism provided by the database to generate a primary key, a database for generating primary keys in sequence (e.g., sequence of Oracle, DB2, etc.) to generate a unique identity for long/short/int columns, such as:< Generator class= "sequence" ><param name= "sequence" > sequence name </param></generator> such as Oracle Sequence

L Uuid.hex: Generated by hibernate based on the 128-bit unique value algorithm, based on the current device IP, time, JVM start time, internal self-increment, etc. 4 parameters to generate a hexadecimal value (after the encoded length of 32-bit string representation) as the primary key. Even in the case of multiple instances running concurrently, this algorithm ensures the uniqueness of the resulting ID to the maximum extent possible. Of course, the probability of repetition still exists theoretically, but the probability is relatively small. In general, generating primary keys using the Uuid.hex method provides the best data insertion performance and data platform adaptability.

L Uuid.string: Similar to Uuid.hex, just encodes the generated primary key (16 bits in length). A problem 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 subsequent table correspondence.

L Select:hibernate 3 introduces the new primary key generation mechanism, which mainly focuses on the renovation of legacy systems.

Due to the common database, such as SQL Server, MySQL and so on, provide an easy-to-use primary key generation mechanism (such as the Auto-increase field), the primary key generation mechanism provided by the database, the native generator can be used to configure the primary key generation mode.


Four, the element structure and attributes of the map file

1, root element:, each hbm.xml file has a unique root element that contains some optional attributes

[HTML]View PlainCopyprint?
  1. <hibernate-mapping package="Com.demo.hibernate.beans">
  2. <class name="User" ... >
  3. </hibernate-mapping>
  4. <hibernate-mapping>
  5. <class name="Com.demo.hibernate.beans.User" ... >
  6. </hibernate-mapping>

(1). Package: Specifies the full name of the persistent class for the current mapping file (for example: package= "Entity.dao")
(2). Schema: Specifies the schema name of the database table corresponding to the current mapping file
(3). Catalog: Specifies the catalog name of the database table that corresponds to the current mapping file
(4). Default-cascade: Set the default cascading method (default = None)
(5). Default-access: Sets the default property access method (default value)
(6). Default-lazy: Set to deferred loading for mapping classes and collections that do not specify lazy loading (the default is True)
(7). Auto-import: Sets whether a non-full class name can be used in HQL in the current mapping file (the default value is True)

byte point of the hibernate-mapping node:
(1). Class: Specifies the corresponding persistent class name and corresponding database table name for the current mapping file
(2). Subclass: Specifies the subclass of the persisted class that corresponds to the current mapping file when a polymorphic persistence operation is specified
(3). Meta: Setting metadata properties for a class or property
(4). typedef: Setting a new Hibernate data type
(5). Joined-subclass: Specifies the subclass of the current junction
(6). Union-subclass: Specifies the subclass of the current junction
(7). Query: Define a HQL query
(8). Sql-query: Define a SQL query
(9). Filter-def: Specify Filter

2,<class> defines a class : A child element of the root element that defines the mapping of a persisted class to a data table, as follows some optional attributes that the element contains

(1). Name: Specify the corresponding persistent class name for the current mapping file
(2). Table: Specify the corresponding database table name for the current mapping file
(3). Schema: Sets the schema name of the database table corresponding to the currently specified persistence class
(4). Catalog: Sets the catalog name of the database table that corresponds to the currently specified persistence class
(5). Lazy: Set whether to use lazy loading
(6). Batch-size: Set the number of batch operation records (default is 1)
(7). Check: Specify a SQL statement for the pre-schema condition check
(8). Where: Specify the Where condition for an additional SQL statement
(9). ROWID: Specifies whether to support ROWID
Entity-name: Entity Name default value is class name
(one). Subselect: Mapping immutable read-only entities to a subquery in a database
Dynamic-update: Specifies whether the SQL statement used for update dynamically generates a default value of False
Dynamic-insert: Specifies whether the SQL statement used for insert dynamically generates a default value of False
Insert-before-update: Sets whether the object is actually modified by the SELECT statement before Hibernate performs the update, and if the value of the object is not changed, the UPDATE statement will not be executed (the default value is False )
Abstract: A superclass used to identify abstractions in a federated subclass (the default value is False)
Emutable: Indicates whether an instance of this class is a mutable default value of Fals
Proxy: Specify the deferred load proxy class
Polymorphism: Specifies how the polymorphic query is used by default to implicit
Persister: Specify a Persister class
Discriminator-value: Subclass Recognition Identity Default value is class name
Optimistic-lock: Specifies the policy default value for optimistic locking is vesion
byte point of the class node:
(1). ID: Defines the primary key properties of the persisted class for the current mapping file and information about the primary key fields in the data table
(2). Property: Defines the properties of the persisted class for the current mapping file and information about the fields in the data table
(3). Sql-insert: Perform an insert operation with a custom SQL statement
(4). Sql-delete: Perform a delete operation using a custom SQL statement
(5). Sql-update: Perform an update operation using a custom SQL statement
(6). Subselect: Define a subquery
(7). Comment: Define a table's annotations
(8). Composite-id: The Federated primary key for the persistent class that corresponds to the database table
(9). Many-to-one: Define a Many-to-one association between objects
One-to-one: Defining a one-to-one correlation between objects
(one). Any: Define the any mapping type
(). Collection mappings for MAP:MAP types
(). Collection mappings for Set:set types
(). Collection mappings for list:list types
(). Collection mappings for Array:array types
(+). Collection mappings for Bag:bag types
(+). Collection mappings for Primitive-array:primitive-array types
Query: Define a HQL statement that loads an entity
Sql-query: Defines the SQL statement that loads the entity
Synchronize: Define the synchronization resources required by the persistence class
Query-list: Maps The collection returned by the query
Natural-id: Declares a unique business primary key
Join: Map the properties of a class to multiple tables
Sub-Class: Declaring subclasses in a polymorphic map
Joined-subclass: Connecting subclasses in the life polymorphism map
(+). Union-subclass: Declaring a federated subclass in a polymorphic map
Loader: The loader that defines the persisted object
Filter: Define the filters used by hibernate
Component: Define Component Mappings
(). Dynamic-component: Defining dynamic Component Mappings
(+). Properties: Defines a logical grouping that contains multiple attributes
Cache: Define a policy for caching
Discriminator: Define a discriminator
Meta: Set metadata properties for a class or property
Timestamp: Specify the data in the table that contains the timestamp
Vesion: Specifies the data included with version information for the table

4,<id> defining the 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, and hibernate maintains the corresponding relationship between the Java object and the records in the database table at run time, based on the OID.

Attributes of the ID node:
(1). Name: Specifies the primary key name of the persisted class for the current mapping
(2). Column: Specifies the primary key name in the database table corresponding to the current mapping (default is the primary key/attribute name for the corresponding persistent class)
(3). Type: Specifies the data type of the primary key in the database table that corresponds to the current mapping
(4). Unsaved-value: Determine if this object has been saved
(5). Daccess:hibernate the policy for accessing primary key properties (default is property)

properties of the 5.generator node:
(1). Class: Specify the primary key generator
(2). Name: Specifies the primary key name of the persisted class for the current mapping
(3). Column: Specifies the primary key name in the database table corresponding to the current mapping (the default 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 mapping
(5). Unique: Sets the value of the field to be unique (default is False)
(6). Not-null: Sets whether the value of this field can be null (the default is False)
(7). Update: Set whether the update operation contains data for this field (the default value is True)
(8). Insert: Sets whether the insert operation contains data for this field (the default value is True)
(9). Formula: The value of this property is evaluated with the specified SQL when the query operation is set
(access:hibernate). The policy to access this property (default value is properties)
(one). Lazy: Sets whether the field takes a lazy load policy (the default value is False)
Optimistic-lock: Specifies whether optimistic locking is required for this property to do an update operation (the default value is True)

Properties of the 6.property node:
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 type:hibernate mapping type

      (4). Formula: Sets the value of the property in the persisted class for the current node to be fetched from the database by the specified SQL
NOTE: The specified SQL must be enclosed in (). Specifies that columns in SQL must be accessed using a table alias plus a column name, but if you specify a column in SQL that you want to use for the current mapping, you cannot use the table alias Plus. Add column names to access, but direct access to
  such as: Formula= "(select Tn.columnname from TableName tn where tn.columnname= attribute names in current mappings) "
      (5). Unique: Sets whether the value of the field is unique ( The default is False)
      (6). Not-null: Sets whether the value of this field can be null (the default value is False)
      (7). Not-found: Sets how the data referenced when the database field corresponding to the current node does not exist when it is not present (the default is exception: An exception is generated, the optional value is ignore: associated to a nonexistent app to null)
      (8). Property-ref: Sets the property name of the associated class, which corresponds to the default value of the associated class's primary key
      (9). Entity-name: Entity name of the associated class
     . Lazy: Specifies whether to use a lazy load and load policy (the default is proxy: An optional value of true: This object takes a lazy load and fetches when the variable is first accessed, false: This association object does not take lazy loading)
      (one). Access:hibernate the policy that accesses this property (the default value is properties)
      (). Optimistic-lock: Specifies whether optimistic locking is required when this property is being updated (the default value is True)

Properties of the

7.one-to-one node:
      (1). Name: The name of the mapping class property
      (2). Class: Name of the Association class
      (3). Formula: Most one-to-one associations point to the primary key of their entity. In some cases, you point to one or more fields or an expression, and you can use an SQL formula to represent
      (4). Cascade: Cascading type when setting cascading actions
      (5). Constrained: Indicates whether a FOREIGN key constraint exists between the table that corresponds to the current class and the table being associated   The default value is False
      (6). Fetch: Sets the policy for fetching data to an optional value of Join OUTER JOIN FETCH, select sequence selection crawl
      (7). Property-ref: Sets the property name of the associated class, which corresponds to the primary key of the class relative to the default value of the associated class
      (8). Access:hibernate The policy that accesses this property (default value)
      (9). Lazy: Specifies whether to use lazy loading and loading policies The default value is proxy to associate with an optional value of true This object takes a lazy load and crawls when the variable is first accessed, false this associated object does not take the lazy load
      (TEN). Entity-name: The entity name of the associated class


8.many-to-one element:
      (1). Name: The name of the mapping class property
      (2). class : Association class name
      (3). Formula: Most one-to-one associations point to the primary key of their entity. In some cases, you point to one or more fields or an expression, and you can use an SQL formula to represent
      (4). Column: The Intermediate association table is mapped to the associated field of the Target association table
       (5). Cascade: Cascading type
      (6) When cascading operations are set. Fetch: Set the policy default value for fetch data to select the selection of the FETCH option to fetch the optional value for the join outer join crawl
      (7). Lazy: Specifies whether to use the default value of deferred load and load policy to associate through proxy optional value true This object takes a lazy load and fetches when the variable is accessed for the first time, false this association object does not take lazy load
      (8). Update: Whether this field is included in the update operation
      (9). Insert: Contains this field when an insert operation is made
      (not-found): Specifies how the data referenced by the foreign key does not exist when the processing default value of exception produces an exception an optional value of ignore associated to a nonexistent app to null

9.many-to-many elements
10.set elements
(1). Name: Mapping the names of class properties
(2). Table: Associated Target database table
(3). Schema: schema name of the target database table
(4). Catalog: Catalog name of the target database table
(5). Subselect: Define a subquery
(6). Sort: Sets the type of sort default to unsorted the optional value is natural natural sort, Comparatorclass implements the interface class as the sorting algorithm to avoid simultaneous use with order-by
(7). Lazy: Whether to use lazy loading
(8). Inverse: Used to identify the passive side of a bidirectional association the default value is False
(9). Cascade: Cascading types when setting cascading operations
Mutable: Identifies whether the associated object can change the default value to True
(one). Order-by: Setting collation
Where: Increase filter criteria
Batch-size: The default value for the number of read data at a time of lazy loading is 1
Fetch: Set the policy of fetching data optional value for join outside join FETCH, select sequence Pick crawl
11.list elements
12.map elements

Xxx.hbm.xml Detailed summary of mapping files in hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.