Spring @Transactional无法在独立应用程序中运行

时间:2015-03-31 00:55:23

标签: java spring hibernate jpa

我有一个独立的应用程序,我正在尝试填充数据库。 我的应用程序配置如下:

@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:classpath property file" })
@ComponentScan(basePackages = { my packages to scan })
public class PersistenceJPAConfig {    

    @Bean
    public EntityManagerFactory entityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { packages to scan});

        JpaVendorAdapter vendorAdapter = jpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        em.afterPropertiesSet();

        return em.getObject();
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.ORACLE);
        return jpaVendorAdapter;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(url);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryBean());

        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        return new Properties() {
            { // Hibernate Specific:
                setProperty("hibernate.hbm2ddl.auto", "validate");
                setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
            }
        };
    }
}

但是,当我尝试在数据库中保留某些内容时,注释失败并显示错误:     线程“main”中的异常javax.persistence.TransactionRequiredException:没有事务性EntityManager可用

在生成的错误堆栈中,我没有看到正在应用的事务性建议。

包含事务注释的文件是这样的:

import org.springframework.transaction.annotation.Transactional;

@Service
public class MyService {

    @Autowired
    private MyDao myDao;

    @Transactional
    public void save(MyEntity e) {
        myDao.save(e);
    }
}

MyDao课程是这样的:

@Repository
public class MyDao {
    @PersistenceContext
    private EntityManager entityManager;

    public void save(MyEntity e) {
        entityManager.persist(e);
    }

}

我从我的Main方法调用函数如下:

public static void main(String[] args) {

    try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
        ctx.scan(CONFIG_PACKAGE);
        ctx.refresh();
        myService = ctx.getBean(MyService.class);
        Thread.sleep(10 * 1000);
        myService.save(entity);
        ...
}

堆栈跟踪看起来像这样(编辑以删除与我的代码有关的太具体的跟踪):

Exception in thread "main" javax.persistence.TransactionRequiredException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
at com.sun.proxy.$Proxy27.persist(Unknown Source)
at mypackage.MyDao.save(MyDao.java:42)
at mypackage.MyDao$$FastClassBySpringCGLIB$$758201c8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at mypackage.MyDao$$EnhancerBySpringCGLIB$$9bd3b4fc.save(<generated>)
at mypackage.MyService.save(MyService.java:176)
at mypackage.MyService$$FastClassBySpringCGLIB$$5c0a03f6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
at mypackage.Run.main(Run.java:109)

任何人都可以帮我解决这个问题,即我可能做错了什么? 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您需要在配置类中添加@EnableJpaRepositories(basePackages = "base package")注释。