13 (Batch processing)
Hibernate 100 000
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
}
tx.commit();
session.close();
50 000 (OutOfMemoryException) 。 Hibernate (Customer) session 。
。 , , JDBC (batching) 。 JDBC (batch size) ( ,10-50 ):
hibernate.jdbc.batch_size 20 , identiy ,Hibernate JDBC 。
:
hibernate.cache.use_second_level_cache false , , CacheMode 。
13.1. (Batch inserts)
, flush() clear() 。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) {
//20, same as the JDBC batch size //20, JDBC
//flush a batch of inserts and release memory:
//
session.flush();
session.clear();
}
}
tx.commit();session.close();
13.2. (Batch updates)
。 , , scroll() 。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session.getNamedQuery("GetCustomers").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
Customer customer = (Customer) customers.get(0); customer.updateStuff(...);
if ( ++count % 20 == 0 ) {
//flush a batch of updates and release memory: session.flush();
session.clear();
}
}
tx.commit();
session.close();
13.3. StatelessSession ( session)
,Hibernate API, detached object , 。StatelessSession , 。 , session cache, , 。 , 。 stateless session 。stateless session (Collections)。 stateless session Hibernate 。 session , 。 session , JDBC 。
StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session.getNamedQuery("GetCustomers").scroll(ScrollMode.FORWARD_ONLY);
while ( customers.next() ) {
Customer customer = (Customer)
customers.get(0);
customer.updateStuff(...);
session.update(customer);
}
tx.commit();
session.close();
, Customer (detach)。 。
StatelessSession insert(), update() delete() , INSERT, UPDATE DELETE 。 , Session save(), saveOrUpdate() delete() 。
13.4. DML( ) (DML-style operations)
hence manipulating (using the SQL Data Manipulation Language (DML) statements: INSERT, UPDATE, DELETE) data directly in the database will not affect in-memory state. However, Hibernate provides methods for bulk SQL-style DML statement execution which are performed through the Hibernate Query Language ( 14 HQL: Hibernate ). , / (object/relational mapping) 。 , ( SQL Data Manipulation Language(DML, ) :INSERT ,UPDATE DELETE) 。 ,Hibernate Hibernate ( 14 HQL: Hibernate ) SQL DML 。
UPDATE DELETE : ( UPDATE | DELETE ) FROM? EntityName (WHERE where_conditions)? :
FROM (from-clause) ,FROM
FROM (from-clause) , 。 , ; , 。
HQL 14.4 “join ”( )。 WHERE 。 where , join。
WHERE 。
, Query.executeUpdate() HQL UPDATE (: ( JDBC's PreparedStatement.executeUpdate()):
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate();
tx.commit();
session.close();
HQL UPDATE , 5.1.7 “ (version)( )” 5.1.8 “timestamp ( )” 。 EJB3 。 , versioned update, Hibernate version timestamp 。 UPDATE VERSIONED 。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlVersionedUpdate = "update versioned Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate ).setString( "newName", newName ).setString( "oldName", oldName ) .executeUpdate();
tx.commit();
session.close();
, (org.hibernate.usertype.UserVersionType) update versioned 。
HQL DELETE, Query.executeUpdate() :
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlDelete = "delete Customer c where c.name = :oldName"; // or String hqlDelete = "delete Customer where name = :oldName"; int deletedEntities = s.createQuery( hqlDelete ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit(); session.close(); Query.executeUpdate() 。 ( SQL ) “ ” , 。 HQL SQL , , joined-subclass 。 。 joined-subclass , “ ” , joined-subclass 。
INSERT : INSERT INTO EntityName properties_list select_statement. :
INSERT INTO ... SELECT ... , INSERT INTO ... VALUES ... .
properties_list SQL INSERT (column speficiation) 。 , properties_list 。 ; 。 ,INSERT 。
select_statement HQL , 。 , , 。 , HibernateType (equivalent) (equal), 。 org.hibernate.type.DateType org.hibernate.type.TimestampType , 。
id ,insert 。 properties_list id ( select ), properties_list ( )。 id ; “ ” , 。 , , org.hibernate.id.SequenceGenerator( ), org.hibernate.id.PostInsertIdentifierGenerator 。 org.hibernate.id.TableHiLoGenerator, , 。
version timestamp ,insert , properties_list ( select ), properties_list ( , org.hibernate.type.VersionType seed value( ))。
HQL INSERT :
Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();String hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...";int createdEntities = s.createQuery( hqlInsert ) .executeUpdate();tx.commit();session.close();