Summary of important rules of hibernate

Source: Internet
Author: User
Tags flush inheritance table name
rules for writing entity classes

L entity classes must have a non-parametric construction method

L entity classes must have a database identity

L typically use a non-business-meaningful logical primary key as the database identifier, usually the int/long/string type

L typically generate database identity values from hibernate

n the more common strategies include:

U int/long or Short type: identity,sequence,hilo,native

U String Type: UUID

U manual Assignment: assigned

u use in one-to-one correlation mapping: Foreign

Session Management of entity objects

An instantaneous object is an object that has no database identity and is not managed by any session, and the database does not have a corresponding record.

L Persistent object is managed by a session, and must have a database ID, and have corresponding records in the database

L offline objects are database identifiers, objects that are not managed by the session, and have corresponding records in the database

The Save method is used to convert an entity object from an instantaneous state to a persisted state

The L Update method is used to convert an entity object from an offline state to a persisted state

L Saveorupdate method is automatically judged by hibernate

n if the object is offline (with the database identity), the update operation is performed automatically

n if the object is in a transient state (no database identity value), the save operation is performed automatically

The Flush method is used to force the state of the persisted object managed by the session in memory to be synchronized to the database (issue insert/update/delete statement)

The L evict method is used to convert a persisted object to an offline object

The L Clear method is used to convert all persisted objects managed in the session to offline objects

The L merge method is used to copy the attribute value of an offline object to the persisted object corresponding to it.

L Hibernate will delay the issuance of SQL statements as much as possible (Insert/update/delete/select)

L no more than two entity objects of the same type with the same database identity are allowed in the same session

L object in persisted state, not allowed to refer to an instantaneous object

The lazy load policy is only valid during session opening (that is, only for entity objects that are in the persisted state), and if an attempt is taken to obtain an attribute that has not yet been loaded in an offline object, a lazy load exception will be thrown.

Association Mappings

l about equals and hashcode methods

n If you need to manage the entity objects in the collection, you need to override the Equals and Hashcode methods

n When overriding both methods, if the database identity is automatically generated (non-assigned policy), it is best not to override it based on the database identity.

n It is best to generate the Equals and Hashcode methods based on attributes with the following characteristics in the entity class

U Non-empty

U Unique

L in a one-to-many bi-directional association, it is common to set the end of "inverse=" to "true" ( which means we usually maintain the association on the "many" end )

N Inverse property only for bidirectional correlation

n and can only be set at the end of the "one" (at the end of "many" there is no inverse attribute at all), in other words, on the many end, is always can maintain the association

L in a one-to-many bidirectional association, the name of the other key field remains consistent

L in Hibernate, the collection type of the entity class uses the interface definition (for example: Set, List, Map) instead of the specific implementation type (e.g. HashSet, ArrayList, HashMap)

L Many-to-many associations are typically split into two many-to-one associations

L Limitations of single-table inheritance mappings

Attributes of n subtypes cannot be defined as not-null= "true"

L Specific Class table inheritance (union-subclass) mapping policy limitations

n cannot use identity generation policy. hql Query

L HQL is an object-oriented query statement that uses the class name, the property name to query (not the table name, the field name) in the HQL statement

L Note case sensitivity of HQL statements

N class names and attribute names are case sensitive, and other characters (such as keywords: Select/from/where/order by GROUP by) are case insensitive

L use to represent the index parameter, assign a value to the parameter, and assign a value starting at the No. 0 index.

L Use ": Parameter name" method, in the HQL statement, define named parameters, assign values, directly by name assignment

L Null value query, it is recommended to use is NULL to determine null value

L can use a collection (or an array) as a parameter to the HQL statement, note the use of the Setparameterlist method

The N collection (or array) cannot be null or empty

The parameter type passed must match the data type of the property

L in the HQL statement, you cannot use select* from ... Such a syntax.

L Projection Query (query part properties)

N If multiple properties are queried, the elements in the returned list are object[] array types

N If a single property is queried, the element in the returned list is the type that corresponds to the property

The statistical functions in the SQL syntax can be used directly in the HQL statement

L If there is at most one record in the result set of the query, you can call the Uniqueresult method from query to remove the object directly

L can convert a query result set to a variety of different types of objects

N is converted to an entity class (note that the appropriate construction method needs to be defined)

U For example: select New Person (p.id,p.name,p.address) from person p

n converts to other types of Vo objects (note that the full path class name is used and the appropriate construction method is defined)

U For example: Select New Cn.com.leadfar.vo.PersonVO (p.id,p.name) from person p

N is converted to the map type (each Map object represents a record, the alias of the attribute is the key in the Map object, and the value of the property is the corresponding value)

U For example: Select New Map (p.id as pid,p.name as pname) from person p

n Conversion to list type

U For example: select New List (p.id,p.name) from person p

L Multi-State query

N in the HQL statement, all objects belonging to this base type can be isolated by the name of the base type directly

n can also be queried through an interface or abstract class

L Paging Query

n Query.setfirstresult starting from the first few records

n Query.setmaxresults maximum return of several records

L Query.iterate () method

N If the query is an entity object, Hibernate will first send a statement query ID list, and when iterating over the entity object in it, hibernate again queries the entity object (n+1 query problem) by issuing the SQL statement individually based on the ID.

N If the query is not an entity object (such as a query statement such as select P.id,p.name from the person p that returns a result object that is not an entity object), there is no problem with the n+1 query

L Query.scroll () method, return scrollableresults, query with support of database cursor

L Conditional Query: If you use example objects, it is more convenient to build query conditions

L Connection Query

n Intra-Connection query

U explicit INNER JOIN query: Using the JOIN keyword in a query statement

U implicit INNER JOIN query: Do not use the JOIN keyword in query statements, but instead use object navigation to query

n Outer JOIN query

U LEFT Outer connection query: In addition to the data that satisfies the connection condition, it will also query all the data of the left table.

u right Outer connection query: In addition to the data that satisfies the connection condition, it will also query all the data of the right table.

Caching Policies

L Cache Level

N-level caching is Hibernate's built-in cache policy and cannot be canceled, but it can be managed

N-Level caching is session-level caching and disappears as session closes

The data cached by the N-level cache is the entity object

The entity objects loaded by the Get/load method are bound to be placed in the first level cache

The entity objects that you query through HQL statements are also placed in the first level cache

The entity object in the N-level cache is actually an entity object in the persisted state.

L first-level cache management

n Save/update/get/load causes the object to be added to the first level cache

N Flush operation causes synchronization of the state of an object in memory (that is, in the first level cache) to the database

n the refresh operation causes the corresponding records in the database to be synchronized in memory (that is, in the first-level cache).

N evict/clear action to move an object in the first-level cache out of a first-level cache to become an offline object

L Level Two cache

N Level two cache needs to be enabled and the cache policy provider is specified

N Level two cache is managed by sessionfactory and does not disappear as the session closes

L Query Cache

n Query caching also needs to be enabled, and specify the cache policy provider (and level two cache using the same policy provider)

n before querying, you need to call Query.setcachable (true) to use level two caching.

Crawl Strategy

L Basic Concepts

n What is called a crawl strategy. A policy that loads an entity object and how to get its associated object.

n What is called bulk fetch. That is, the query to a batch of entity objects, want to get the objects associated with this batch.

L set fetch on single-ended association (MANY-TO-ONE)

n fetch= "Select", query Fetch, which is the default value, which means that when an entity object is loaded, hibernate emits a second query statement when it wants to get its associated object.

n fetch= "join", which means that when a physical object is loaded (load or get), its associated object is loaded at the same time through the SQL statement of the LEFT outer join, which in the instance of this project means that the person object is loaded while The associated group object (if any) is already loaded, so the lazy defined on Many-to-one will expire ... That is, the associated object no longer delays loading.

L Batch Crawl with single-ended association (MANY-TO-ONE)

n query to a batch of entity objects, in order to get the object of the single-ended association object, if these objects have not been loaded, hibernate will issue a second query to query its associated objects, so that the issue of the query may be more. We can set batch-size to improve the bulk fetch performance of these related objects, can be configured on the other's class mapping configuration batch-size,hibenate will automatically based on this value, the bulk of fetching the associated objects.

L Set FETCH on collection Association

n fetch= "Select", query Fetch, which is the default value, meaning that an entity object is loaded, and if you want to get its associated object (stored in a collection), Hibernate emits a second query statement

n fetch= "join", which means that, while loading (load or get) an entity object and loading its associated object (stored in a collection) at the same time through the SQL statement of the LEFT outer join, the lazy mechanism will fail in this case.

L Collection-associated bulk fetch

n queries to a batch of entity objects, in order to get the object's associated objects (stored in a collection), if those associated objects have not been loaded, hibernate will issue other query statements to query its associated objects. So what query statements will hibernate make to query these associated objects?

u If you set fetch= "select" On the collection

Hibernate will query its associated objects one after another using the second query statement

u If you set fetch= "select" and Batch-size on the collection

L Hibernate will query its associated objects through several batch query statements

u If you set fetch= "Subselect" on the collection

Hibernate will query the associated object of this batch of entity objects through a single query statement (this query statement contains a subquery statement), which is generated based on the query statement of the query entity object.

L " Note: After setting the fetch=" join ", the lazy load policy on the association will expire " other

L version for optimistic locking strategy

L Proper adjustment of hibernate.jdbc.batch_size and hibernate.jdbc.fetch_size parameters can greatly improve the performance of high-volume data processing

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.