Hibernate Learning---section III: Mapping configuration file Details

Source: Internet
Author: User
Tags mysql in

1, the map file, the code is as follows:

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><!--persisting the mapping file (mapping Java objects to database tables) Default-access= Property Object Properties State operation mode Setting the property indicates that the attribute needs to be manipulated by the Getter/setter method ( The default field means that you can manipulate properties by reflection without the Getter/setter method to specify the package path for the middle class of the persisted configuration file default-lazy= "Tru E "Specifies the medium-class deferred load policy for persistent profiles default-cascade=" None "Specifies the class-in-class cascading action policy for the persisted configuration file -<hibernate-mapping Package= "Learn.hibernate.bean"default-access= "Property"Default-cascade= "None">        <!--1. Name (optional): the Java fully qualified name of the persisted class (or interface). If this property does not exist, hibernate will assume that this is a non-pojo entity mapping 2, table (optional-default is the non-fully qualified name of the Class): Corresponding database table name, data table name when generating DDL, if omitted, name with persisted class name 3, lazy= "true" Extended load (default); Lazy= "false" immediately load 4, dynamic-insert= "true" property without assignment does not participate in new action 5, dynamic-update= "true" property not assigned does not participate in Update operation 6, Mutable= "false" means that the persisted class does not allow updates, but can add and remove 7, select-before-update= "true" before initiating an SQL query to see if the data is consistent with the table, inconsistent updates, and no updates (default False) 8, where query is accompanied by the condition in the Where attribute -    <classname= "Person"Table= "T_person">        <!--          The primary Key Name property value corresponds to the property name of the persisted class; the column (optional) property value represents the class value in the database-generated field name generator element that represents the build strategy 1) Assigned: Primary                        The key is generated by an external program and is specified before save ().                        2) Hilo: The primary key generation mechanism implemented by the HI/LO algorithm requires additional database tables or fields to provide high-level value sources.                        3) Seqhilo: Similar to Hilo, the primary key generation mechanism implemented by the HI/LO algorithm requires a Sequence in the database, which is appropriate for a database that supports Sequence, such as Oracle. 4) Increment: The primary key is incremented by numerical 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 it cannot be used under a cluster. 5) Identity: Use the primary key generation mechanism provided by the database.                        such as DB2, SQL Server, MySQL in the primary key generation mechanism. 6) Sequence: Use the sequence mechanism provided by the database to generate the primary key.                        such as the sequence in Oralce.                        7) Native: According to the database used by Hibernate, it uses identity, Hilo and sequence as the primary key generation mode.                        8) Uuid.hex: A 128-bit UUID algorithm is used by Hibernate to generate 16 binary values (encoded with a string representation of length 32) as the primary key.                        9) Uuid.string: Similar to Uuid.hex, only the generated primary key is not encoded (length 16) and cannot be applied in the PostgreSQL database.          Foreign: Use the identifier of another associated object as the primary key. -        <IDname= "id"column= "person_id">            <Generatorclass= "Native"/>        </ID>                <!--name (optional): The property name of the corresponding Java class, column (optional): The field in the corresponding table type (optional): The type of the property, Eg.java.lang.String not-null (available Selected): Sets whether the property is empty, is true when non-null, and defaults to false length (optional): Length limit for a field -        < Propertyname= "Name"column= "T_name"/>        < Propertyname= "Age"/>            < Propertyname= "Passwork"/>        < Propertyname= "Birthday"/>        <!--The data for the specified attribute count is derived from the formula corresponding query statement, and Count does not have a corresponding field in the database. -        < Propertyname= "Count"Formula= "(select COUNT (*) from T_person)"/>    </class>    </hibernate-mapping>

2, 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, such as:

<class name= "User" ...>

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

(2), schema: the 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 properties and collection classes that do not explicitly indicate the lazy attribute, and what default load style hibernate takes, 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

3. <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 properties that the element contains

(1) Name: the Java fully qualified name of the persisted class (or interface), and hibernate assumes that this is a non-pojo entity mapping If this property does not exist

(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 expression

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, decision-making strategy for 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 an abstraction superclass

4. <id> define 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 policy for accessing attribute values

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>

5, <generator> set 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: the Java application is responsible for generating identifiers. The primary key is generated by an external program and is specified before save ().

(2) Hilo:hibernate Branch High/low algorithm generates identifiers

(3) Seqhilo: Use a high/low algorithm to efficiently generate identifiers of type Long,short or int

(4) Increment: Hibernate is automatically generated incrementally, the implementation mechanism of this method is to maintain a variable in the current application instance to hold the current maximum value, and then each time you need to generate a primary key to the value of 1 as the primary key. The possible problem with this approach is that it cannot be used under a cluster.

(5) Identity: generated by the underlying database identifier. such as DB2, SQL Server, MySQL in the primary key generation mechanism

(6) Sequence:hibernate generates identifiers based on the sequence of the underlying database, which requires the underlying database to support the sequence

(7) Native: Automatically select identity, sequence, or Hilo based on how the underlying database automatically generates identifiers.

(8) Uuid.hex:Hibernate uses a 128-bit UUID algorithm to generate identifiers

(9) Uuid.string:UUID is encoded into a 16-character string

Foregin: An identifier for an object that is associated with another

(View: http://blog.csdn.net/martin820303/article/details/7769625)

6. <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: 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 that the instance variable is accessed for the first time. If this property is delayed fetching, the default is False

(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

(Ten) Not-null: Use DDL to add a nullable constraint

(one) Optimistic-lock: Specifies whether this property needs to be optimistic when updating (in other words, It determines whether the value of version versions grows when this property has dirty data) 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 property 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)"/>

(From: http://blog.163.com/hzd_ love/blog/static/13199988120108265317988/)

Hibernate Learning---section III: A detailed mapping configuration file

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.