gpt4 book ai didi

java - 如何在 Spring 3 的 Java 配置中连接我的 Hibernate 4 拦截器?

转载 作者:行者123 更新时间:2023-12-02 06:12:01 26 4
gpt4 key购买 nike

我有一个 Hibernate 拦截器,我想将其放在 Hibernate 的 onLoad() 调用上。我这样做是因为我希望 JPA 实体有一个由 Spring 注入(inject)的 SecureRandom 实例。由于 JPA 上下文和 Spring 上下文不混合,因此这是从 Spring 上下文到 JPA 上下文的桥梁。

我的 Java 配置中有“两个地方”,我在其中设置了 Hibernate 4 的内容。我在下面包含了它们的条目。根据此( https://jira.springsource.org/browse/SPR-8940 ),我认为要以编程方式设置 Hibernate 拦截器,我需要访问 LocalSessionFactoryBean。也许通过 LocalContainerEntityManagerFactoryBean?我只是不知道该怎么做,或者是否需要重新配置我设置 Hibernate 内容的方式。任何帮助将不胜感激!

@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;
}
}

最佳答案

我是这样处理的:

Spring数据库配置类:

@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拦截器类:

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

关于java - 如何在 Spring 3 的 Java 配置中连接我的 Hibernate 4 拦截器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com