Hibernate use important notes

tags: Hibernate  JDBC  Oracle  MySQL  SQL Server

Hibernate Usage Important Notes

Hibernate API introduction:
Configuration class

SessionFactory interface: It is best to create only one factory (unless it is a multi-data source)

Transaction interface

Query, Criteria interface: execute the database query, act as Hibernate querier

Hibernate commonly used identity generator
[quote]assigned

hilo

increment

identity

sequence

native[/quote]

* MySQL and SQL Server prioritize identity
* Oracle prioritizes the use of sequence
** If you want to improve the portability of your application, choose a native generator for cross-platform applications.

Conversion of three states:
Temporary objects, persistent objects and free objects

[img]http://dl.iteye.com/upload/attachment/149492/2306e85e-e491-34f2-a5df-df529e9a0f30.gif[/img]

Figure 1:


Important methods about Session:
beginTransaction();

close();

Connection () : **** This method directly obtains the JDBC connection, bypassing Hibernate to directly access the database, the addition, deletion, and generally will be more efficient than Hibernate.

delecte();

Get( Class entityClass,Serializable id); If there is no corresponding instance, it will return null

Load( Class entityClass,Serializable id);//If there is no corresponding instance, it will throw an exception.

save(Object entity);

update(Object entity)

saveOrUpdate(Object entity)

isInitialized() and initialize() ;/// can solve the problem of lazy loading better.


QBC query method: (Query By Criteria )
* Common methods in the Restrictions class to set query conditions
* Order class Order.asc ascending order, Order.desc descending
* Projections class Statistics and grouping results (Projections.avg,..)

//1
Criteria criteria = session.createCriteria(User.class);
List list = criteria.list();

//2 pagination
criteria.add(Restriction.eq("id",new Integer(1)));
criteria.setMaxResults(10);
User user = (User) criteria.uniqueResult()//Load a single object

criteria.addOrder(Order.desc("id");)

//3 joint query
Criteria cusCriteria = session.createCriteria(Customer.class);//
Criteria ordCriteria = cusCriteria.createCriteria("orders");//
ordCriteria.add(Restrictions.gt("money",new Double(1000)));
...

//4 offline query (bind to the session only when the query is executed) (usually used to dynamically build query statements)
/ / In the presentation layer constructs the query statement
DetachedCriteria dCritera = DetachedCriteria.forClass(Order.class);
dCritera.add(Restrictions.gt("money",new Double(1000)));
....
serviec.queryOrders(dCritertia);

Session session = ....
//In the business layer
Critertia criteria = dCritertia.getExecutable(session);
criteria.list();



Bulk insert:
Method 1: Set hibernate.jdbc.batch_size to 50 (integer) and recommend to turn off the second level cache.

 for(int i=0;i<1000;i++){
....
session.save(customer);
if(i%50 ==0 ){
Session.flush () ;/ / synchronize this batch of data with the database
Session.clear () ;/ / clear the buffer area, release memory to attack a batch of use
}
}



Method 2: Bypassing Hibernate using JDBC


Batch update
Method 1: Configuration
<property name="hibernate.query.facotry_class">  
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</property>

Method two.; use JDBC

batch deletion:


Multi-data source application:
....

Intelligent Recommendation

Use of !important

The front-end css style uses the vue framework, and its own css style is duplicated with the custom name, causing the style to be overwritten. original custom style, Custom style after modification, W...

Summary of hibernate-hibernate notes

The notes are from the hibernate course of the horse soldier teacher. 1, class to table @table specifies the table name @column specifies the field name @Transient specifies fields that do not need to...

A brief overview and use of JAVA's important reflection mechanisms-Xiaobai notes

Article Directory Overview of reflection mechanisms The role of reflection: Specific and popular understanding of reflection dynamics: Main APIs related to reflection Specific use of reflection: Dynam...

Python study notes: the use of more important parameters in pandas.read_excel()

sheet_name The name, string/number of the sheet you want to open; the following operations are equivalent headerWhich row is the column label row index_col Which column is used as the row label (index...

More Recommendation

Hibernate notes a

1. Overview of the frame 1.1. What is the framework Frame: refers to the semi-finished software to complete some of the features. 2.EE three-tier architecture 2.1 EE classic three-tier structure: 3 .H...

a hibernate notes

Beginner hibernate, configure appear after running 1、hibernate.dialect 'must be set when no Connection available error; Check the configuration file, write it right just do not know what went wrong, d...

Hibernate notes (a)

A, web review JavaEE three-tier structure: 1, web layer: Struts2 2, service layer: spring 3, dao layer: Hibernate (complete change search database additions and deletions) Second, the idea of ​​unders...

[Notes] hibernate

1.hibernate of five core interfaces Interface configuration Configure hibernate hibernate open instance of the object created sessionFactory SessionFactory interfaces Initializing a hibernate sessionF...

hibernate-notes

Annotation example One-to-many Many-to-one ---------------Employee.java----------------- package com.huarui.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persi...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top