Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement interceptor and listener with Hibernate

2025-01-18 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report