简体   繁体   English

Spring 数据@CreatedDate 注释对我不起作用

[英]Spring Data @CreatedDate annotation doesn't work for me

I am working on project where I use Spring Data.我正在从事使用 Spring 数据的项目。 I wanted to fill in creationTime field using @CreatedDate annotation instead using method with @PreUpdate or @PrePersist annotation (doing it this way it works perfectly).我想使用@CreatedDate注释而不是使用带有@PreUpdate@PrePersist注释的方法来填充creationTime字段(这样做效果很好)。 When I do it with @CreatedDate it just leaves this field blank.当我使用@CreatedDate时,它只会将该字段留空。 I use postgresql database.我使用 postgresql 数据库。 Documentation is not very helpful. 文档不是很有帮助。

Do you have any idea how can I fix it?你知道我该如何解决吗? Thank you!谢谢!

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 :我的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>

I may have been in a similar situation where I wanted the Spring Data JPA @CreatedDate annotation to work, but had no need for the user-level auditing that is otherwise described in their documentation. 我可能一直处于类似的情况,我希望Spring Data JPA @CreatedDate注释能够工作,但不需要在文档中描述的用户级审计。

To get the annotation-based auditing to work, I had to nonetheless add a class to my project that implemented org.springframework.data.domain.AuditorAware . 为了使基于注释的审计工作,我不得不在我的项目中添加一个实现org.springframework.data.domain.AuditorAware This is odd because you don't actually seem to use the value returned from the getCurrentAuditor() method that you'll be implementing; 这很奇怪,因为你实际上似乎并没有使用你将要实现的getCurrentAuditor()方法返回的值; mine just returns null . 我只是返回null

public class NullAuditorBean implements AuditorAware {

    @Override
    public Object getCurrentAuditor() {
        return null;
    }
}

I then needed to reference my "null object" AuditorAware implementation in an entry in my applicationContext to activate the JPA auditing. 然后我需要在applicationContext一个条目中引用我的“null对象” AuditorAware实现来激活JPA审计。 I had to make sure I did this before the line that specifies the jpa:repositories . 我必须确保在指定jpa:repositories的行之前执行此操作。 This looks something like: 这看起来像:

<bean id="auditorBean" class="your.package.subbed.here.NullAuditorBean"/>
<jpa:auditing auditor-aware-ref="auditorBean"/>

I also had to add an orm.xml file, and needed to formally reference it as a property of my entityManagerFactory bean, like so: 我还必须添加一个orm.xml文件,并且需要正式引用它作为我的entityManagerFactory bean的属性,如下所示:

<property name="mappingResources">
    <value>META-INF/orm.xml</value>
</property>

Make sure this META-INF/orm.xml entry is stored with your compile output (mine is in my WAR under WEB-INF/classes . 确保此META-INF/orm.xml条目与您的编译输出一起存储(我的WAR在WEB-INF/classes下的WAR中。

That orm.xml file, for the record, contained some boilerplate, which can be found in the answer to this related question . 对于记录,该orm.xml文件包含一些样板文件,可以在相关问题的答案中找到。

It was a fair amount of work when I got this working. 当我开始工作时,这是相当多的工作。 You may prefer your previous working solution! 您可能更喜欢以前的工作解决方案!

I have experienced the same issue, but wasn't able to get my head around it.我遇到过同样的问题,但无法解决这个问题。 I have chosen to use Hibernate's @CreationTimestamp instead, and it works like a charm!我选择改用 Hibernate 的@CreationTimestamp ,它的效果非常好!

This question is quite old, but still relevant. 这个问题很古老,但仍然相关。 For me the key was this, from the documentation 对我来说,关键是这个,来自文档

Since Spring Data MongoDB 1.4 auditing can be enabled by annotating a configuration class with the @EnableMongoAuditing annotation. 从Spring Data开始,可以通过使用@EnableMongoAuditing批注对配置类进行批注来启用MongoDB 1.4审计。

For example: 例如:

@Configuration
@EnableMongoAuditing
class Config {

  /**
   * Optional, depending on your needs
   */
  @Bean
  public AuditorAware<AuditableUser> myAuditorProvider() {
      return new AuditorAwareImpl();
  }
}

Or, in XML: 或者,在XML中:

<mongo:auditing/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @CreatedDate 注解不适用于 spring-data-elasticsearch - @CreatedDate annotation does not work with spring-data-elasticsearch 简单的Spring @CreatedDate注释对我不起作用 - Simple Spring @CreatedDate annotation is not working for me Spring Data MongoDB - 使用自定义 Id 字段时,注释 @CreatedDate 不起作用 - Spring Data MongoDB - Annotation @CreatedDate does not work while using with custom Id field 手动分配 ID 时,Spring Data MongoDB Annotation @CreatedDate 不起作用 - Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually Spring数据:带有批注@Query的方法无法正常工作 - Spring Data: Method with annotation @Query doesn't work as expected @CreatedDate 注释不适用于 mysql - @CreatedDate annotation does not work with mysql Spring Profile无法与Aspect注释一起使用 - Spring Profile doesn't work with Aspect annotation CrossOrigin注释不适用于Spring Security - CrossOrigin annotation doesn't work with spring security Spring @ComponentScan注释不起作用 - Spring @ComponentScan annotation doesn't work Spring控制器上的Aop注释不起作用 - Aop Annotation at Spring Controllers Doesn't Work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM