In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement interceptors and listeners in Hibernate. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Interceptors and events are both extension mechanisms of Hibernate, and Interceptor interface is the old implementation mechanism, which is now changed to event listening mechanism; they are all callback interfaces of Hibernate, and Hibernate is calling save, delete, update.... This class is called back when these methods are used.
The interceptor before Hibernate3.0 is very similar to the filter in java.
Hibernate3.0 later changed the interceptor to event listening.
SaveOrUpdateEventListener interface:
It will be monitored by Hibernate when saving and updating data.
Example:
If (event.getObject () instanceof com.cos.User) {
/ / if this event manipulates the User object, execute the code in if
}
Tell the Hibernate the listener:
Listener SaveListener.java:
Java code
Package com.cos.listener; import org.hibernate.HibernateException; import org.hibernate.event.SaveOrUpdateEvent; import org.hibernate.event.SaveOrUpdateEventListener; public class SaveListener implements SaveOrUpdateEventListener {@ Override public void onSaveOrUpdate (SaveOrUpdateEvent event) throws HibernateException {if (event.getObject () instanceof com.cos.entity.User) {System.out.println ("Save actions before User") }
Hibernate profile:
Xml code
"- / / Hibernate/Hibernate Configuration DTD 3.0//EN"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> org.hibernate.dialect.MySQLDialectproperty > com.mysql.jdbc.Driverproperty > jdbc:mysql:///testproperty > property > property > trueproperty > createproperty > Event > session-factory > hibernate-configuration >
You can see two listeners, one is your own listener and the other is the default listener. Which of the two listeners will be executed first.
This is the end of the article on "how to implement interceptors and listeners in Hibernate". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.