为什么拦截器的 onLoad() 不起作用?

发布于 2024-07-24 22:05:31 字数 2889 浏览 3 评论 0原文

我们有一个基于 jboss 的系统

persistance.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="solutions" transaction-type="JTA">
        <jta-data-source>java:/mam</jta-data-source>

        <class>....</class>
        ......
        <class>....</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>

            <property name="hibernate.connection.datasource" value="java:/mam"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/solutions"/>

            <property name="hibernate.ejb.interceptor"
                      value="interceptor.AuditAndDeletableCatcherInterceptor"/>

            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.generate_statistics" value="false"/>

            <property name="hibernate.bytecode.use_reflection_optimizer" value="cglib"/>
            <property name="hibernate.dialect" value="com.magenta.componentization.audit.sql.MySQL5CustomDialect"/>
            <property name="hibernate.query.substitutions" value="true 1, false 0"/>
            <property name="hibernate.connection.provider_class"
                      value="org.hibernate.connection.DatasourceConnectionProvider"/>
            <property name="hibernate.current_session_context_class" value="thread"/>

            <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        </properties>
    </persistence-unit>

</persistence>

拦截器的代码:

public class AuditAndDeletableCatcherInterceptor extends AuditInterceptor {

    DeletableCatcherDeligate deletableCatcherDeligate =
            new DeletableCatcherDeligate();

    @Override
    public boolean onLoad(Object o, Serializable serializable, Object[] objects, String[] strings, Type[] types) {
        deletableCatcherDeligate.onLoad(o, serializable, objects, strings, types);
        return super.onLoad(o, serializable, objects, strings, types);
    }
}

其中 AuditInterceptor 扩展了本机 hibernate 的 EmptyInterceptor 并重载一些方法,如 onSave()、onFlush()、onPreFlush()

AuditAndDeletableCatcherInterceptor 的一些方法可以工作,但 onLoad() 永远不会被调用。 我究竟做错了什么?

We have a jboss based system

persistance.xml looks like a following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="solutions" transaction-type="JTA">
        <jta-data-source>java:/mam</jta-data-source>

        <class>....</class>
        ......
        <class>....</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>

            <property name="hibernate.connection.datasource" value="java:/mam"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/solutions"/>

            <property name="hibernate.ejb.interceptor"
                      value="interceptor.AuditAndDeletableCatcherInterceptor"/>

            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.generate_statistics" value="false"/>

            <property name="hibernate.bytecode.use_reflection_optimizer" value="cglib"/>
            <property name="hibernate.dialect" value="com.magenta.componentization.audit.sql.MySQL5CustomDialect"/>
            <property name="hibernate.query.substitutions" value="true 1, false 0"/>
            <property name="hibernate.connection.provider_class"
                      value="org.hibernate.connection.DatasourceConnectionProvider"/>
            <property name="hibernate.current_session_context_class" value="thread"/>

            <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        </properties>
    </persistence-unit>

</persistence>

Interceptor's code:

public class AuditAndDeletableCatcherInterceptor extends AuditInterceptor {

    DeletableCatcherDeligate deletableCatcherDeligate =
            new DeletableCatcherDeligate();

    @Override
    public boolean onLoad(Object o, Serializable serializable, Object[] objects, String[] strings, Type[] types) {
        deletableCatcherDeligate.onLoad(o, serializable, objects, strings, types);
        return super.onLoad(o, serializable, objects, strings, types);
    }
}

Where AuditInterceptor extends native hibernate's EmptyInterceptor
and overload some methods like onSave(), onFlush(), onPreFlush()

Some methods of the AuditAndDeletableCatcherInterceptor work, but onLoad() is never called.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-07-31 22:05:31

仅当从数据库而非缓存中检索对象时才会调用 onLoad。 如果该对象已被管理,则不会调用 onLoad。 您可以通过实现PrepareStatement 并查看其作用来测试这一点。 我所做的就是实现所有方法并在编写拦截器时将它们全部记录下来,然后我就确切地知道它在做什么。

onLoad is only called if the object is being retrieved from the database and not the cache. if the object is already managed, then the onLoad is not called. You can test this by implementing PrepareStatement and seeing what its doing. What I do is implement all the methods and log them all when writing an interceptor, then I know exactly what its doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文