从数据库加载我的模型后,我当前正在尝试使用@PostLoad注释进行某些处理。但是目前看来我的方法不会被触发。我不使用EntityManager,所以我正在寻找一种启用此事件行为的方法。

我的配置如下所示:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider
        </property>
        <property name="hibernate.connection.tinyInt1isBit">true</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testing</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- OUTPUT STUFF -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_sql_comments">false</property>
        <property name="hibernate.format_sql">false</property>
        <!-- SESSION CONTEXT -->
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- CONNECTION POOL hikariCP -->
        <property name="hibernate.hikari.maximumPoolSize">25</property>
        <property name="hibernate.hikari.idleTimeout">30000</property>
        <property name="hibernate.hikari.dataSource.url">jdbc:mysql://localhost:3306/testing</property>
        <property name="hibernate.hikari.dataSource.user">root</property>
        <property name="hibernate.hikari.dataSource.password"></property>
        <property name="hibernate.hikari.dataSource.cachePrepStmts">true</property>
        <property name="hibernate.hikari.dataSource.prepStmtCacheSize">250</property>
        <property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit">2048</property>
        <property name="hibernate.hikari.dataSource.useServerPrepStmts">true</property>
        <!-- Include the mapping entries -->
        <mapping class="at.adtime.core.v1.model.User"/>
        <mapping class="at.adtime.core.v1.model.Test"/>
    </session-factory>
</hibernate-configuration>


更新:

我有一个名为afterUserLoad的方法,它看起来像这样:

@PostLoad
public void afterUserLoad() {
    ArrayList<String> computedIds = new ArrayList<String>();
    for (Test test : this.Test) {
        computedIds.add(test.getId());
    }
    this.setTestIds(computedIds);
}


它应该加载Test List并将仅ids放在ArrayList中。

最佳答案

根据我的研究,@PostLoad@PrePersist和其他类型的这些JPA注释不适用于Hibernate,可以使用Interceptor代替它们。我正在使用基于XML的配置的Hibernate-JPA和Spring。使用Spring我得到了一个SessionFactory对象,当我设置该对象的属性时,我注入了扩展MyInterceptor类的EmptyInterceptor实例,那么您可以覆盖postLoadonSave等几种方法。实施此代码后,将根据操作的生命周期执行覆盖的方法。我没有测试postLoad方法,因为onSave方法对我有用。

更新资料

基于Spring Java的配置@JeffersonTavares:

Spring配置类:

@Configuration
public class SpringConfig {

    @Bean
    public BasicDataSource basicDataSource(){ // org.apache.commons.dbcp2.BasicDataSource Object
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        basicDataSource.setUrl("jdbc:sqlserver://172.125.25.7:1433;databaseName=LIST_FIRMAS;selectMethod=direct;");
        basicDataSource.setUsername("user");
        basicDataSource.setPassword("password");
        return basicDataSource;
    }

    @Bean
    public MyInterceptor myInterceptor(){  // Your interceptor
        return new MyInterceptor();
    }

    @Bean(name = "sessionFactory")
    public SessionFactory sessionFactory() throws IOException{
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();

        localSessionFactoryBean.setDataSource(basicDataSource()); // DataSource

        localSessionFactoryBean.setAnnotatedClasses(
                com.isapeg.timbrado.model.SalMinimo.class,
                com.isapeg.timbrado.model.DeduccionCnom12.class,
                com.isapeg.timbrado.model.PercepcionCnom12.class); // JPA entity classes ....

        localSessionFactoryBean.setEntityInterceptor(myInterceptor()); // Setting your Interceptor

        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
        properties.put("hibernate.current_session_context_class", "thread");
        properties.put("hibernate.show_sql", "false");
        localSessionFactoryBean.setHibernateProperties(properties);  // Setting Hibernate Properties

        localSessionFactoryBean.afterPropertiesSet();  // Session Factory initialization

        return localSessionFactoryBean.getObject();  // Returning of SessionFactory Object
    }

    // other beans ....
}


测试会话工厂对象的东西:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
if (sessionFactory == null) {
    System.out.println("SessionFactory obj is null");
} else {
    Session session = sessionFactory.openSession();
    System.out.println("Session obj: " + session);
    System.out.println("isOpen: " + session.isOpen() + "  isConnected: " + session.isConnected());
    session.close();
}


更新2

如何实现拦截器here

07-25 23:29