Hibernate Batch Data Processing

Source: Internet
Author: User

Batch Data Processing refers to processing a large amount of data in a transaction.

You can perform batch operations at the application layer by using the following methods:

Through Session
Using HQL
Use StatelessSession
Use the jdbc api (as long as this is used, others are used for understanding)

The save () and update () Methods of batch operation sessions through sessions will store the processed objects in their own cache. if you use a Session object to process a large number of persistent objects, you should promptly clear the objects that have been processed and will not be accessed from the cache. the specific method is to call the flush () method to refresh the cache immediately after processing an object or a small batch of objects, and then call the clear () method to clear the cache

Processing through sessions is subject to the following constraints: you need to set the number of JDBC batch processing in the Hibernate configuration file, ensure that the number of SQL statements sent to the database each time is consistent with the batch_size attribute. If the object adopts the "identity" identifier generator, Hibernate cannot perform batch insert operations on the JDBC layer for batch operations, we recommend that you disable the second-level cache batch update of Hibernate: When batch update is performed, if all objects are loaded to the Session cache and updated one by one in the cache, it is obviously not desirable.

Use a scrollable result set Z contains? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/LqsyM66zrbUz/keys/LLFu + G1vcr9vt2/4tbQvNPU2M/g06a1xLbUz/keys =" http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090324200.png "alt =" \ ">


Batch operation through HQL
Note: HQL only supports insert... INSERT statement in SELECT format, but insert into… is not supported... Therefore, HQL cannot be used for batch insert.

Using StatelessSession to Perform Batch operations. The usage of StatelessSession is similar to that of session. Compared with session, StatelessSession has the following differences:
StatelessSession is not cached. objects loaded, saved, or updated through StatelessSession are in the Free State.
StatelessSession does not interact with Hibernate's second-level cache.
When you call the save (), update (), or delete () Methods of StatelessSession, these methods immediately execute the corresponding SQL statement instead of executing only one SQL statement.
StatelessSession does not perform a dirty check. Therefore, after modifying the attributes of the Customer object, you must call the update () method of StatelessSession to update data in the database.
StatelessSession does not perform any cascade operations on the associated objects.
The same StatelessSession object loads the Customer object whose OID is 1 twice, and the memory address of the two objects is different.
Operations performed by StatelessSession can be captured by the Interceptor, but will be ignored by the Hibernate event processing system.

Public class HibernateTest {private SessionFactory sessionFactory; private Session session; private Transaction transaction; @ Beforepublic void init () {Configuration configuration Configuration = new Configuration (). configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (configuration. getProperties ()). buildServiceRegistry (); sessionFactory = configuration. buildSessionFactory (serviceR Egistry); session = sessionFactory. openSession (); transaction = session. beginTransaction () ;}@ Afterpublic void destroy () {transaction. commit (); session. close (); sessionFactory. close () ;}@ Testpublic void testBatch () {session. doWork (new Work () {@ Overridepublic void execute (Connection connection) throws SQLException {// perform operations through the JDBC native API with the highest efficiency and the fastest speed !}});}}







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.