5

I am trying to create a Spring MVC application leveraging JPA for its persistence layer. Unfortunately, I was getting a NullPointerException when accessing the EntityManager as Spring does not appear to be injecting it. My configuration is all annotation-based with @EnableWebMvc. After some searching, I added @Transactional on my DAO and @EnableTransactionManagement on my @Configuration class. Then I got an error about not having a DataSource. Supposedly, a class with @EnableTransactionManagement needs to implement TransactionManagementConfigurer. However, I am having problems figuring out how to create the DataSource as well as why it cannot get it from my persistence.xml.

I would greatly appreciate any help in trying to get the EntityManager injected into my DAO.

My @Configuration class

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.example.myapp")
public class MvcConfig extends WebMvcConfigurerAdapter 
        implements TransactionManagementConfigurer {

private static final boolean CACHE_ENABLED = true;
private static final String TEMPLATE_PATH = "/WEB-INF/freemarker";
private static final String TEMPLATE_SUFFIX = ".ftl";

private static final Logger LOG = Logger.getLogger( MvcConfig.class );

@Override
public void addResourceHandlers( ResourceHandlerRegistry registry ) {
    registry.addResourceHandler( "/stylesheets/**" ).addResourceLocations( "/stylesheets/" );
}

@Bean
public FreeMarkerConfigurer configureFreeMarker() {
    final FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath( TEMPLATE_PATH );
    return configurer;
}

@Bean
public ViewResolver configureViewResolver() {
    final FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setCache( CACHE_ENABLED );
    resolver.setSuffix( TEMPLATE_SUFFIX );
    return resolver;
}

@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new DataSourceTransactionManager();
}

}

My DAO

@Component
@Transactional
public class MyDAO {

    private static final Logger LOG = Logger.getLogger( MyDAO.class );

    @PersistenceContext
    private EntityManager entityManager;

    public MyClass getMyClass() {
        LOG.debug( "getMyClass()" );
        final CriteriaQuery<MyClass> query = criteriaBuilder.createQuery( MyClass.class );
        // more code here, but it breaks by this point
        return myData;
    }

}

My Updated Code

I have reached the point in which it almost all works. The EntityManager is being injected properly. However, transactions are not working. I get errors if I try to use a RESOURCE_LOCAL approach so I am looking at JTA managed transactions. When I add @Transactional on any of my DAO methods, I get a "Transaction marked for rollback" error with no further details in any log files to assist troubleshooting. If I remove the annotation from a basic read-only select, the select will work perfectly fine (not sure if I should even be putting the annotation on select-only methods). However, I obviously need this working on methods which perform db writes. If I debug through the code, it seems to retrieve the data perfectly fine. However, as it returns from the method, the javax.transaction.RollbackException gets thrown. From my understanding of everything, it seems as if the exception occurs in the AOP post-method processing.

My @Configuration class

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.example.myapp")
public class MvcConfig extends WebMvcConfigurerAdapter {

private static final boolean CACHE_ENABLED = true;
private static final String TEMPLATE_PATH = "/WEB-INF/freemarker";
private static final String TEMPLATE_SUFFIX = ".ftl";

private static final Logger LOG = Logger.getLogger( MvcConfig.class );

@Override
public void addResourceHandlers( ResourceHandlerRegistry registry ) {
    registry.addResourceHandler( "/stylesheets/**" ).addResourceLocations( "/stylesheets/" );
}

@Bean
public FreeMarkerConfigurer configureFreeMarker() {
    final FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath( TEMPLATE_PATH );
    return configurer;
}

@Bean
public ViewResolver configureViewResolver() {
    final FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setCache( CACHE_ENABLED );
    resolver.setSuffix( TEMPLATE_SUFFIX );
    return resolver;
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new JtaTransactionManager();
}

@Bean
public AbstractEntityManagerFactoryBean entityManagerFactoryBean() {
    LocalEntityManagerFactoryBean factory = new LocalEntityManagerFactoryBean();
    factory.setPersistenceUnitName( "my_db" );
    return factory;
}

}
4

2 回答 2

9

在我的应用程序中,我没有实现 TransactionManagerConfigurer 接口。我使用下一个代码来配置 JPA(使用 Hibernate 实现)。您可以在配置类中执行相同的操作。

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean factoryBean = 
                new LocalContainerEntityManagerFactoryBean();

        factoryBean.setDataSource(dataSource());
        factoryBean.setPackagesToScan(new String[] {"com.dimasco.springjpa.domain"});

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(true);
        //vendorAdapter.setGenerateDdl(generateDdl)

        factoryBean.setJpaVendorAdapter(vendorAdapter);

        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.hbm2ddl.auto", "update");

        factoryBean.setJpaProperties(additionalProperties);


        return factoryBean;
    }

    @Bean
    public DataSource dataSource() {
        final ComboPooledDataSource dataSource = new ComboPooledDataSource();

        try {
            dataSource.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }

        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setMinPoolSize(3);
        dataSource.setMaxPoolSize(15);
        dataSource.setDebugUnreturnedConnectionStackTraces(true);

        return dataSource;
    }

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

        return transactionManager;
    }

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

希望对你有帮助)

编辑:

您可以使用 JNDI 查找来获取数据源:

@Bean
public DataSource dataSource() throws Exception {
   Context ctx = new InitialContext();
   return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}

您可以在本文中找到更多详细信息。有 JndiDatasourceConfig 类的示例。

编辑 2: 我在我的项目中使用了 persistence.xml,但它是空的:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="JPA_And_Spring_Test">
    </persistence-unit>
</persistence>

而且我没有在我的 java 配置中指定任何持久单元名称。

于 2013-02-10T18:31:21.553 回答
1

以下可能会有所帮助,即使它使用基于 XML 的配置:

https://github.com/springinpractice/sip13/blob/master/helpdesk/src/main/resources/spring/beans-repo.xml

它使用 Spring Data JPA,但您不必这样做。利用

 @PersistenceContext private EntityManager entityManager;

(但请考虑 Spring Data JPA,因为它提供了非常强大的开箱即用的 DAO。)

旁注:对于 DAO,@Repository支持@Component. 两者都适用于组件扫描,但@Repository更好地描述了预期用途。

于 2013-02-12T18:37:02.223 回答