Siege Lion on the road (one) Hibernate (vii)---manipulate objects by hibernate (bottom)

Source: Internet
Author: User


Oneworking together with triggers
When hibernate works in conjunction with a database trigger, the following two types of problems occur:
1.triggers make inconsistencies in the data in the session cache and in the database
This problem occurs because the trigger is running inside the database, and the action it performs is transparent to the session.
Solution: After executing the action containing the trigger, immediately invoke the flush () and Refresh () method of the session, forcing the session's cache to synchronize with the database.
2.Session Update () method blind excitation trigger
This occurs primarily when the session calls the update () or Saveorupdate () method, enabling a free object to be associated with the session, because a snapshot of the object does not exist in the cache, so an UPDATE statement is definitely executed to fire the trigger. If the object's properties are consistent with the data in the database, then the UPDATE statement is redundant. To avoid this situation, set select-before-update= "true" in the <class> element.
Twogenerate audit logs with interceptors: No concern at this stage.
ThreeHibernate event handling mechanism
The core processing module of HIBERNATE3 uses the "event \ Listener" design mode.
Most methods of the session trigger a specific event, which is handled by the appropriate listener.
The event classes and listener interfaces that correspond to the various methods of the session are provided in the Org.hibernate.event package.
General steps for creating and registering a custom listener method:
1.Create a custom listener
A, directly implement a specific listener interface.
B. Inherit the underlying implementation class of the listener interface provided by hibernate, such as Org.hibernate.enent.def.AbstractSaveEventListner.
C. Inherit the default implementation class of the listener interface provided by Hibernate. Like Org.hibernate.event.def.DefaultSaveEventListner.
2.registering a customized listener: 2 Different ways
A, register statically in Hibernate's profile hibernate.cfg.xml.

?
1 2 3 <event type="load"> <listener class="mypack.MyLoadListener" /></event>

B, dynamic Registration in the program: can register multiple.

?
1 2 3 Configuration cfg = new Configuration(); LoadEventListener[] listeners = {new MyLoadListener(),...};cfg.getEventListeners().setLoadEventListeners(listeners);

FourBulk processing of data
Problems with bulk operations:
A, the use of large amounts of memory, such as batch updates, you need to load a large number of objects into memory, and then update each.
B, too many update statements are executed.
Therefore, you should try to avoid bulk operations at the application layer, but you should do the bulk operations directly at the database level. To perform bulk operations at the application tier, there are four ways to do this:
1.batch operation via session
This is done after you have processed an object or a small batch of objects, call the Flush () method immediately to clean up the cache, and then call the clear () method to empty the cache.
Need to note:
Set the Hibernate.jdbc.batch_size property appropriately.
If an object uses the identity identifier generator, hibernate cannot do bulk insert operations on the JDBC layer.
It is recommended to turn off level two cache when doing bulk operations.
2.bulk operation via Statelesssession: Usage and session are similar.
3.bulk operation via HQL: Query.executeupdate ()
4.batch operations directly through the JDBC API
Fiveworking with Meta data
The Sessionfactory.getclassmetadata () and getcollectionmetadate () can be used to manipulate the metadata by acquiring the corresponding object.
SixExecuting stored procedures: See how JDBC is.

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.