Basic mapping of Hibernate

Source: Internet
Author: User
Tags access properties db2

Description of the configuration file hibernate-mapping is the root element of the hibernate mapping file

Schema: Specifies the name of the mapped database schema. Specifying this property indicates that the schema prefix is added automatically
Catalog: Specifies the name of the mapped database catalog.
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
Auto-import (Default true): Specifies whether a non-fully qualified class name can be used in a query language (restricted to classes in this mapping file).
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.

class element

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 the INSERT statement contains only those fields that are not 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. 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. This property is required when using the <subclass/> element to define an inheritance relationship for a persisted class


Hibernate uses object identifiers (OIDs)

Used to establish correspondence between in-memory objects and records in database tables. The OID of the object corresponds to the primary key of the data table. Hibernate assigns a value to the primary 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. The <generator> child element is used to set the identifier 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 (first name of the primary key field) of the data table to which the IDENTITY property is mapped.
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

Several ID generators

The increment identifier generator increments the surrogate primary key by Hibernate, Hibernate reads the maximum value of the primary key in the news table, and then inserts the record into the news table, incrementing it on the basis of Max (ID), which is 1 increments. Scope of application:

    • Because the increment survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems
    • For applications where only a single Hibernate application process accesses the same database, it is not recommended for use in a clustered environment
    • The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

The identity identifier generator is responsible for generating identifiers by the underlying database, which requires the underlying database to define the primary key as the autogrow field type
Scope of application:

    • Because the identity generation identifier mechanism relies on the underlying database system, it is required that the underlying database system must support the Autogrow field type. Databases that support autogrow field types include: DB2, Mysql, MSSQLServer, Sybase, etc.
    • The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

The sequence identifier generator uses the sequence provided by the underlying database to generate identifiers.

Hibernate when persisting an object, first obtains a unique identification number from the NEWS_SEQ sequence of the underlying database, and then takes it as the primary key value

Scope of application:
Because the mechanism of sequence generates identifiers relies on the sequence of underlying database systems, it is required that the underlying database system must support sequences. Supported sequences of databases include: DB2, Oracle, etc.
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

The Hilo identifier generator is generated by Hibernate according to a high/low algorithm * that gets the high value from the field of a particular table in the database.

Hibernate is responsible for generating the primary key value when a News object is persisted by hibernate. When the Hilo identifier generator generates an identifier, it needs to read and modify the Next_value value in the Hi_table table.
Scope of application:
Because the Hilo Survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

The native identifier generator chooses to use the identity, sequence, or Hilo identifier generator, depending on the underlying database's ability to automatically generate identifiers.
Scope of application:
Because native can automatically select the appropriate identifier generator based on the type of underlying database system, it is well suited for cross-database platform development
The OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time

Property

The property element is used to specify the properties of the class and the mapping of the table's fields
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, and so on.
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 you use the Formula property
Formula= "(SQL)" cannot have fewer parentheses
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.

Datetime

A Java type may correspond to multiple Hibernate mapping types. For example, if the properties of a persisted class are of type Java.util.Date, the corresponding Hibernate mapping type can be Date, time, or timestamp. The Hibernate mapping type must be determined based on the SQL type of the corresponding data table field. If the field is of the date type, then the Hibernate mapping type is date; If the field is of type time, then the Hibernate mapping type is time; If the field is timestatmp type, the Hibernate mapping type is timestamp.


<!--time (year/month/day, specific time)--
<property name= "Date" type= "date"/>
<property name= "Time" type= "Time"/>
<property name= "timestamp" type= "timestamp"/>

Boolean

<!--boolean--
<property name= "isused" type= "Yes_no" >
<column name= "isused" sql-type= "varchar (1)" ></column>
</property>

<!--enum--
<property name= "Gender" >
<column name= "Gender" >
</column>
<type name= "Org.hibernate.type.EnumType" >
<param name= "Enumclass" >com.fengye.hibernate.basic.Gender</param>
<param name= "Type" >12</param>
</type>
</property>

Clob

<property name= "Content" >
<column name= "Content" sql-type= "Mediumtext"/>
</property>

Blob

<property name= "image" >
<column name= "IMAGE" sql-type= "Mediumblob" ></column>
</property>

Component Mappings

There are different starting points for establishing a domain model and a relational data model:
Domain model: consists of program code, which improves the reusability of code by refining the granularity of the persisted class, simplifying the programming

In the absence of data redundancy, you should minimize the number of tables and simplify the referential relationships between tables to improve data access speed

Hibernate divides the properties of a persisted class into two types:
Value type: No OID, cannot be persisted independently, life cycle depends on the life cycle of the object of the owning persisted class
Entity type: With OID, can be persisted independently, with independent life cycle


Obviously cannot directly map pay attributes with property
Hibernate uses the <component> element to map the constituent relationships, the element table name pay property is a component of the Worker class, called a component in Hibernate

<component> elements to map the constituent relationships
Class: Sets the type of the composition relationship property, where the Pay property is pay type


The <parent> element specifies the overall class to which the component properties belong
Name: The property name of the whole class in the component class

<!--component-->
<component name= "component" class= "Com.fengye.hibernate.basic.Component" >
<parent name= "Map"/>
<property name= "Component1" column= "Component1"/>
<property name= "Component2" column= "Component2"/>
</component>

Basic mapping of 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.