title: How do I hook up my Hibernate 4 interceptor in Java configuration for Spring 3?
body: <p>I have a Hibernate interceptor that I want to put on the onLoad() call for Hibernate. I'm doing this because I want a JPA entity to have an instance of SecureRandom injected into it by Spring. Since the JPA context and the Spring context do not mix, this is a bridge from the Spring context into the JPA context.</p>
  
  <p>I have "two places" in my Java config where I setup the stuff for Hibernate 4. I've included their enteries below. According to this (<a href="https://jira.springsource.org/browse/SPR-8940" rel="nofollow">https://jira.springsource.org/browse/SPR-8940</a>) I think that to set the Hibernate interceptor programmatically I need to get access to the LocalSessionFactoryBean. Perhaps through the LocalContainerEntityManagerFactoryBean? I just have no idea how to do that, or if I need to reconfigure the way I'm setting up my Hibernate stuff. Any help would be much appreciated!</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>