Auto generating create date and last modify date using JPA data CrudRepositories

浪子不回头ぞ 提交于 2020-01-02 21:59:33

问题


I'm trying to migrate my app to generate the schema using the entities, instead of having a pre existing schema. Up until now, the create and lastModify dates where updated by the DB (MySql), but now since i want to generate the schema from JPA, i must do this programatically.

Before, i always used @PrePersit and @PreUpdate to do this without any problems but now it doesn't work and i think it's because i use spring data's CrudRepository Interfaces for my DAOs, so i don't know what's going on with the entity manager here...

I did some research and i found a few thing about spring auditing, but i couldn't get it to work. at the moment i'm trying the @Created and @LastModified annotations, but they don't work. this is what i have now: my abstractentity:

@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(name = "seq")
@Column(name = "ID")
private Long id = 0L;

@CreatedDate
// @Generated(GenerationTime.INSERT)
@Column(name = "CREATED", insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;

@LastModifiedDate
@Version
// @Generated(GenerationTime.ALWAYS)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LAST_MODIFIED", insertable = false, updatable = true)
private Date lastModified;

/**
 * copies the auto generated fields id, created and last modified form the given entity/DTO to this entity/DTO
 * 
 * @param copyEntity the entity to copy from.
 */
public void copy(AbstractEntity copyEntity) {
    this.setId(copyEntity.getId());
    this.setCreated(copyEntity.getCreated());
    this.setLastModified(copyEntity.getLastModified());
}

}

my configuration:

@Configuration
@ActiveProfiles("development")
@EnableTransactionManagement
@EnableJpaRepositories
@EnableJpaAuditing(setDates = false, auditorAwareRef = "auditorAware")
public class RepositoryTestContext {

    @Bean
    public AuditorAware<String> auditorAware() {
        return new AuditorAware<String>() {

            @Override
            public String getCurrentAuditor() {
                return "dummy";
            }
        };
    }
}

Basically my tests show that the create date and last modify date are not being updated. any ideas???


回答1:


I think you are missing the @EntityListeners annotation on your AbstractEntity :

@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {



回答2:


I think those annotations are Hibernate 5.2+++

And I think maybe you're using Hibernate 5.0.x or something lower.

I can't tell for sure though without seeing your POM. Just make sure your current version supports this annotation from hibernate.annotations.




回答3:


I think you have set it of in this line: @EnableJpaAuditing(setDates = false, auditorAwareRef = "auditorAware")

This is from Spring docs, Frequently asked questions:

Question: I want to use Spring Data JPA auditing capabilities but have my database already set up to set modification and creation date on entities. How to prevent Spring Data from setting the date programmatically.

Answer:Just use the set-dates attribute of the auditing namespace element to false.



来源:https://stackoverflow.com/questions/28436558/auto-generating-create-date-and-last-modify-date-using-jpa-data-crudrepositories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!