Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据@CreatedDate注释不';我不工作_Java_Spring_Postgresql_Spring Mvc_Jpa - Fatal编程技术网

Java Spring数据@CreatedDate注释不';我不工作

Java Spring数据@CreatedDate注释不';我不工作,java,spring,postgresql,spring-mvc,jpa,Java,Spring,Postgresql,Spring Mvc,Jpa,我正在从事使用Spring数据的项目。我想用@CreatedDate注释来填写creationTime字段,而不是使用带有@PreUpdate或@PrePersist注释的方法(这样做效果很好)。当我使用@CreatedDate执行此操作时,它只会将此字段留空。我使用postgresql数据库。这不是很有帮助 你知道我怎么修吗?谢谢大家! import org.springframework.data.annotation.CreatedDate; @Entity @Table(name = "

我正在从事使用Spring数据的项目。我想用
@CreatedDate
注释来填写
creationTime
字段,而不是使用带有
@PreUpdate
@PrePersist
注释的方法(这样做效果很好)。当我使用
@CreatedDate
执行此操作时,它只会将此字段留空。我使用postgresql数据库。这不是很有帮助

你知道我怎么修吗?谢谢大家!

import org.springframework.data.annotation.CreatedDate;
@Entity
@Table(name = "software")
public class Software implements Serializable {

    // ...

    @Column(name = "creation_time")
    @CreatedDate
    private Date creationTime;
    //...
}
My
applicationContext

<jpa:repositories base-package="path.to.dao"/>


<context:component-scan base-package="path.to.dao"/>
<context:property-placeholder location="classpath:application.properties"/>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driver}"/>
    <property name="url" value="${db.url}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="path.to.bean"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="jpaAdapter"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>

<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

${hibernate.dial}
${hibernate.show_sql}
${hibernate.format_sql}
${hibernate.ejb.naming_strategy}
${hibernate.hbm2ddl.auto}

我可能遇到过类似的情况,我希望Spring数据JPA
@CreatedDate
注释能够工作,但不需要用户级审核,这在他们的文档中有描述

为了使基于注释的审计工作正常,我不得不向我的项目中添加一个类,该类实现了
org.springframework.data.domain.AuditorAware
。这很奇怪,因为您实际上似乎没有使用将要实现的
getCurrentAuditor()
方法返回的值;我的只返回
null

public class NullAuditorBean implements AuditorAware {

    @Override
    public Object getCurrentAuditor() {
        return null;
    }
}
然后,我需要在我的
applicationContext
中的一个条目中引用我的“null对象”
AuditorAware
实现来激活JPA审核。我必须确保在指定
jpa:repositories
的行之前执行此操作。这看起来像:

<bean id="auditorBean" class="your.package.subbed.here.NullAuditorBean"/>
<jpa:auditing auditor-aware-ref="auditorBean"/>
确保此
META-INF/orm.xml
条目与编译输出一起存储(我的在
WEB-INF/classes
下的my WAR中)

记录在案的
orm.xml
文件中包含一些样板文件,可以在对的回答中找到


当我开始工作时,这是一个相当大的工作量。你可能更喜欢你以前的工作解决方案!

这个问题很老了,但仍然相关。对我来说,关键是这个,从

因为Spring数据MongoDB 1.4审计可以通过使用@EnableMongoAuditing注释对配置类进行注释来启用

例如:

@Configuration
@EnableMongoAuditing
class Config {

  /**
   * Optional, depending on your needs
   */
  @Bean
  public AuditorAware<AuditableUser> myAuditorProvider() {
      return new AuditorAwareImpl();
  }
}
@配置
@启用自动编辑
类配置{
/**
*可选,具体取决于您的需要
*/
@豆子
公共AuditorAware myAuditorProvider(){
返回新的AuditorAwareImpl();
}
}
或者,在XML中:

<mongo:auditing/>


你用什么来持久化实体?我的意思是你是直接使用
会话
还是通过Spring数据服务来持久化?我有一个接口
softwarerepository
,它扩展了
JpaRepository
谢谢!它帮助了我。在未来,我将为我的项目添加Spring安全性,然后我将把我的NullAuditor更改为合适的格式。orm.xml修复了基于注释的配置的问题。您可以添加@enableJPaaAuditing(“nullAuditorBean”)
<mongo:auditing/>