SPRING-DATA-JPA简单审计不起作用

时间:2014-01-13 12:06:47

标签: java spring hibernate jpa spring-data

我正在尝试使用spring data jpa提供的审核.Below是我的webappconfig

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.astrika.kernel")
@PropertySource("classpath:application.properties")
@EnableJpaRepositories(basePackages="com.astrika.kernel.repository")
@EnableJpaAuditing

public class WebAppConfig {

private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";


private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

private static final String PROPERTY_NAME_HIBERNATE_POST_INSERT = "hibernate.ejb.event.post-insert";
private static final String PROPERTY_NAME_HIBERNATE_POST_UPDATE = "hibernate.ejb.event.post-update";
private static final String PROPERTY_NAME_HIBERNATE_POST_DELETE = "hibernate.ejb.event.post-delete";
private static final String PROPERTY_NAME_HIBERNATE_PRE_COLLC_UPDATE = "hibernate.ejb.event.pre-collection-update";
private static final String PROPERTY_NAME_HIBERNATE_PRE_COLLC_REMOVE = "hibernate.ejb.event.pre-collection-remove";
private static final String PROPERTY_NAME_HIBERNATE_PRE_COLLC_RECREATE = "hibernate.ejb.event.post-collection-recreate";

@Resource
private Environment env;

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
    dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
    dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
    dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
    entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    entityManagerFactoryBean.setMappingResources("WEB-INF/classes/orm.xml");
     //     entityManagerFactoryBean.setJpaProperties(hibProperties());

    return entityManagerFactoryBean;
}

//  private Properties hibProperties() {
//      Properties properties = new Properties();
//      properties.put(PROPERTY_NAME_HIBERNATE_POST_INSERT, 

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_POST_INSERT));
//      properties.put(PROPERTY_NAME_HIBERNATE_POST_UPDATE, 

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_POST_UPDATE));
//      properties.put(PROPERTY_NAME_HIBERNATE_POST_DELETE, 

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_POST_DELETE));
//      properties.put(PROPERTY_NAME_HIBERNATE_PRE_COLLC_UPDATE,    

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_PRE_COLLC_UPDATE));
//      properties.put(PROPERTY_NAME_HIBERNATE_PRE_COLLC_REMOVE,    

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_PRE_COLLC_REMOVE));
//      properties.put(PROPERTY_NAME_HIBERNATE_PRE_COLLC_RECREATE,  

// env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_PRE_COLLC_RECREATE));
//      return properties;
//  }
 @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new  

HibernateJpaVendorAdapter();

        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
        return hibernateJpaVendorAdapter;
    }

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

@Bean
public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/pages/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

@Bean
public ResourceBundleMessageSource messageSource() {
    ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasename(env.getRequiredProperty("message.source.basename"));
    source.setUseCodeAsDefaultMessage(true);
    return source;
}

我的orm.xml看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" 
    version="2.0">

<persistence-unit-metadata>
    <persistence-unit-defaults>
        <entity-listeners>
            <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
        </entity-listeners>
    </persistence-unit-defaults>
</persistence-unit-metadata>

当我在tomcat服务器上运行我的应用程序时,我收到错误

INFO: HHH000360: Unable to resolve mapping file [WEB-INF/classes/orm.xml]

审计字段如createdOn,lastModifiedOn永远不会获得值。它们始终为null。 请帮忙为什么我得到上面的tomcat错误?它是否负责审计字段的空值?我错过了更多的配置吗?

我忘了提到我正在使用基于注释的审核,即@CreatedOn和 @LastModifiedOn

1 个答案:

答案 0 :(得分:2)

根据定义,必须从类路径引用JPA映射文件。因此,最好将它们保存在src/main/resources/META-INF中。然后可以通过以下方式引用它们:

emfb.setMappingResources("META-INF/orm.xml");

所以我建议移动文件但是如果你将映射资源配置为orm.xml(即剥离WEB-INF/classes前缀),那么使用当前的设置你应该可以使它工作。