如何在Spring 3的Java配置中连接我的Hibernate 4拦截器? [英] How do I hook up my Hibernate 4 interceptor in Java configuration for Spring 3?

查看:96
本文介绍了如何在Spring 3的Java配置中连接我的Hibernate 4拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在我的Java配置中有两个地方我在那里设置了Hibernate 4的东西。我在下面列出了它们的输入。据此( https://jira.springsource.org/browse/SPR-8940 )我认为要以编程方式设置Hibernate拦截器,我需要访问LocalSessionFactoryBean。也许通过LocalContainerEntityManagerFactoryBean?我只是不知道该怎么做,或者如果我需要重新配置我设置Hibernate的东西的方式。任何帮助将不胜感激!
$ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ JpaTransactionManager jpaTransactionManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean){
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(localContainerEntityManagerFactoryBean.getObject());
返回jpaTransactionManager;

$ b $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());

属性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);
返回localContainerEntityManagerFactoryBean;
}


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


解决方案

I放弃了使用一些基于hibernate的基于事件的解决方案的方法,而是使用了@Configuration,它需要Spring中的aspectJ.

请参阅 http://docs.spring.io/spring/docs/3.2.x/spring -framework-reference / html / aop.html


9.8.1使用AspectJ依赖注入域对象与Spring

p>

Spring容器实例化和配置在
应用程序上下文中定义的bean。也可以让一个bean工厂
配置一个预先存在的对象,给定包含要应用的配置的bean定义
的名称。 spring-aspects.jar
包含一个注释驱动的方面,利用这个功能
允许依赖注入任何对象。支持旨在
用于在任何容器控制之外创建的对象。
域对象通常属于此类别,因为它们通常是使用新运算符以编程方式创建的
,或者是由ORM工具以
a数据库查询的结果创建的。



@Configurable注释将类标记为符合
Spring-driven配置。在最简单的情况下,它可以仅用
作为标记注释:


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.

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 (https://jira.springsource.org/browse/SPR-8940) 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!

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

解决方案

I abandoned the approach of using some hibernate specific event based solution and instead went for using @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 to dependency inject domain objects with Spring

The Spring container instantiates and configures beans defined in your application context. It is also possible to ask a bean factory to configure a pre-existing object given the name of a bean definition containing the configuration to be applied. The spring-aspects.jar contains an annotation-driven aspect that exploits this capability to allow dependency injection of any object. The support is intended to be used for objects created outside of the control of any container. Domain objects often fall into this category because they are often created programmatically using the new operator, or by an ORM tool as a result of a database query.

The @Configurable annotation marks a class as eligible for Spring-driven configuration. In the simplest case it can be used just as a marker annotation:

这篇关于如何在Spring 3的Java配置中连接我的Hibernate 4拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆