Spring Data @CreatedDate annotation doesn't work for me
Asked Answered
S

3

17

I am working on project where I use Spring Data. 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). When I do it with @CreatedDate it just leaves this field blank. I use postgresql database. 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:

<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>

Stylish answered 10/12, 2013 at 0:39 Comment(3)
What are you using to persist the entity?Aldora
What I mean is are you using the Session directly or are you persisting through a Spring Data service?Aldora
I've got an interface SoftwareRespository which extends JpaRepository<Software, Long>Stylish
A
14

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.

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. This is odd because you don't actually seem to use the value returned from the getCurrentAuditor() method that you'll be implementing; mine just returns 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. I had to make sure I did this before the line that specifies the 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:

<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.

That orm.xml file, for the record, contained some boilerplate, which can be found in the answer to this related question.

It was a fair amount of work when I got this working. You may prefer your previous working solution!

Arielariela answered 11/12, 2013 at 0:11 Comment(3)
Thanks! It helped me. In the future I'm going to add Spring Security to my project and then I will change my NullAuditor to something suitable.Stylish
orm.xml fixed my problemOutdoors
For annonation based config. You can add @EnableJpaAuditing("nullAuditorBean")Bunker
M
2

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.

For example:

@Configuration
@EnableMongoAuditing
class Config {

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

Or, in XML:

<mongo:auditing/>
Marketplace answered 17/7, 2018 at 22:51 Comment(0)
G
1

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!

Girvin answered 9/3, 2022 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.