Hibernate Object Relational Mapping file

Source: Internet
Author: User
Tags access properties

Brief introduction:

The mapping between the POJO class and the relational database can be defined with an XML document

With the POJO class's database mapping file, Hibernate can understand the relationship between persisted classes and data tables, as well as the relationship between persisted class properties and database table columns.

Hibernate will generate various SQL statements based on this mapping file at run time

The map file has the extension. hbm.xml

Hibernate-mapping:hibernate-mapping is the root element of the mapping file

Default-cascade (default = None): Sets the default cascading style for hibernate. If you configure a Java property, the Cascade attribute is not specified in the collection map, Hibernate takes the cascading style specified here.

Default-access (default property): Specifies the default properties access policy for Hibernate. The default value is property, which is to use getter, setter methods to access properties. If you specify access, Hibernate ignores the Getter/setter method and accesses the member variable through reflection.

Default-lazy (Default is True): Sets the deferred load policy for Hibernat morning. The default value of this property is true, which is to enable the lazy load policy. If you configure a Java attribute map, the lazy attribute is not specified in the collection map, and Hibernate takes the deferred load policy specified here

Package (optional): Specifies a packet prefix, which is used as the package name if no fully qualified class name is specified in the mapping document.

The Class:class element is used to specify the class and table mappings

Name: Specifies the class name of the persisted class for the persisted class map

Table: Specifies the name of this persisted class-map, Hibernate defaults to the class name of the persisted class

Dynamic-insert: Set to True to indicate that an INSERT statement is dynamically generated when an object is saved, and that the INSERT statement contains only all fields with a value other than NULL, the default value is False

Dynamic-update: Set to True to indicate that when an object is updated, an UPDATE statement is dynamically generated, and the UPDATE statement contains only the fields that need to be updated for all values, and the default value is False

Select-before-update: Sets whether Hibernate needs to execute a query first before updating a persisted object. The default value is False

Batch-size: Specifies the number of instances per batch that are fetched by the OID to fetch the instance.

Lazy: Specifies whether lazy loading is used.

Mutable: If set to True, the Update property equivalent to all <property> elements is false, indicating that the entire instance cannot be updated. The default is true.

Discriminator-value: Specifies a value that distinguishes between different subclasses, which is required when using the <subclass/> element to define an inheritance relationship for a persisted class

Mapping Object identifiers:

Hibernate uses object identifiers (OIDs) to establish correspondence between objects in memory and records in database tables, the OID of the object corresponds to the primary key of the data table, and hibernate assigns a value to the main key by using the identifier generator.

Hibernate recommends using a proxy primary key in a data table, which is a field that does not have business meaning. The surrogate primary key is usually an integer type because the integer type saves more database space than the string type.

In the object-relational mapping file, the <id> element is used to set the object identifier,<generator> child elements used to set the identity Fuzhou generator.

Hibernate provides an identifier generator interface: Identifiergenerator, and provides a variety of built-in implementations

ID: Set the OID of the persisted class and the mapping of the table's primary key

Name: Name of the property that identifies the persisted class OID

Column: Sets the name of the data table to which the IDENTITY property is mapped (the name of the primary key field)

Unsaved-value: If this property is set, Hibernate distinguishes whether the object of the current persisted class is a temporary object by comparing the OID value of the persisted class and the property value

Type: Specifies the Hibernate mapping type. The Hibernate mapping type is a bridge between Java type and SQL type. If the mapping type is not explicitly set for an attribute, Hibernate uses the reflection mechanism to identify the Java type of a particular property of the persisted class, and then automatically uses the corresponding default Hibernate mapping type

The basic data type and wrapper type for Java correspond to the same Hibernate mapping type. The base data type cannot express NULL, so it is recommended to use the wrapper type for the OID of the persisted class

Generator: Setting the Persistence class identifier generator

Class: Specifies the identifier generator used to fully qualify the class name or its abbreviated name

Recommended setting to native

The Property:property element is used to specify the properties of the class and the mapping of the table's fields

Name: Specifies the names of the properties of the persisted class

Column: Specifies the field name of the table with the class's property mapping ... If this property is not set, Hibernate directly uses the class's property name as the field name

Type: Specifies the Hibernate mapping type. Hibernate mapping type is a bridge between Java type and SQL type, and if no mapping type is explicitly set for an attribute, Hibernate uses the reflection mechanism to identify the Java type of a particular property of the persisted class, and then automatically uses the corresponding default hibernate Mapping type

Not-null: True if the property value indicates that NULL is not allowed and defaults to False

Access: Specifies the default property access policy for Hibernate. The default value is property, which is to use the Getter,setter method to access the attribute. If field is specified, Hibernate ignores the Getter/setter method and accesses the member variable by reflection

Unique: Sets whether to add a unique constraint for the data column that is mapped by the property.

Index: Specifies the indexed name of a string. When the system needs Hibernate to automatically build the table, it is used to create an index for the data column that the property maps, thereby speeding up the query for that data column.

Length: Specifies the lengths of the fields for the data columns mapped by this property

Scale: Specifies the number of decimal digits of the data column that the property is mapped to, and is valid for data columns of type Double,float,decimal

Formula: Sets an SQL expression that Hibernate will use to calculate the value of the derived property.

Derived property: Not all properties of a persisted class are directly matched to the table's fields, and the values of some properties of the persisted class must be computed at run time, which is called a derived property

When using the Formula property, the parentheses of the Formula= "(SQL) cannot be less

      column names and table names in an SQL expression should correspond to the database, not the properties of the persisted object

      If you need to use parameters in the Formula property, this directly uses the where Cur.id=id form, where the ID is the parameter, and the ID value of the column corresponding to the ID property of the current persisted object is passed in as a parameter.

Hibernate Object Relational Mapping 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.