How do I hook up a Hibernate 4 interceptor in Java configuration for Spring 3?

I have a Hibernate interceptor that I want to use to call onLoad () for Hibernate. I am doing this because I want the JPA entity to have a SecureRandom instance injected into it using Spring. Since JPA context and Spring context do not mix, it is a bridge from Spring context to JPA context.

I have "two places" in my Java config where I am setting up stuff for Hibernate 4. I have included their enteries below. According to this ( https://jira.springsource.org/browse/SPR-8940 ), I think that in order to programmatically program the Hibernate interceptor, I have to access the LocalSessionFactoryBean. Perhaps via LocalContainerEntityManagerFactoryBean? I just don't know how to do this, or if I need to reconfigure the way my sleeper is created. Any help would be much appreciated!

@Bean
JpaTransactionManager jpaTransactionManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(localContainerEntityManagerFactoryBean.getObject());
    return jpaTransactionManager;
}

@Bean(name = "LocalContainerEntityManagerFactory")
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(BasicDataSource jdbcConnection) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    localContainerEntityManagerFactoryBean.setDataSource(jdbcConnection);
    localContainerEntityManagerFactoryBean.setPackagesToScan(this.getClass().getPackage().getName());

    Properties jpaProperties = new Properties();
    jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create");
    localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
    hibernateJpaVendorAdapter.setShowSql(true);
    localContainerEntityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter);
    return localContainerEntityManagerFactoryBean;
}


@Component
public class InvitationEntityInterceptor extends EmptyInterceptor {
    @Autowired
    SecureRandom secureRandom;
    @Override
    public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        if(entity instanceof Invitation) {
            Invitation invitation = (Invitation) entity;
            invitation.setRandom(secureRandom);
        }
        return false;
    }
}

      

0


source to share


2 answers


I ditched the approach using some kind of specific hibernate event based solution and instead went for @Configuration, which requires aspectJ in Spring.

See http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/aop.html



9.8.1 Using AspectJ for Dependencies Builds Domain Objects with Spring

Spring container creates and configures beans defined in your application context. You can also ask the bean factory to configure an existing object named bean definition containing the configuration to use. spring-aspects.jar contains an annotated aspect that uses this feature to enable dependency injection for any object. Support is for objects created outside the control of any container. Domain objects often fall into this category because they are often created programmatically using a new operator or ORM tool as the result of a database query.

@Configurable annotation denotes a class suitable for the spring-given configuration. In the simplest case, it can be used simply as a marker annotation:

0


source


I coped like this:

Spring database configuration class:

@Bean
@DependsOn("hibernateInterceptor")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource,
        JpaVendorAdapter jpaVendorAdapter,
        HibernateInterceptor hibernateInterceptor) {

    LocalContainerEntityManagerFactoryBean emfb =
            new LocalContainerEntityManagerFactoryBean();
    ...
    Properties props = new Properties();
    props.put("hibernate.ejb.interceptor", hibernateInterceptor);
    emfb.setJpaProperties(props);
    return emfb;
}

      



Hibernate interceptor class:

@Component
public class HibernateInterceptor extends EmptyInterceptor {
    ...
    @Autowired
    private MyRepository myRepository;
    ...
}

      

+1


source







All Articles