Auto generating create date and last modify date u

2019-06-02 22:23发布

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

3条回答
再贱就再见
2楼-- · 2019-06-02 22:28

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

@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {
查看更多
疯言疯语
3楼-- · 2019-06-02 22:34

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.

查看更多
Anthone
4楼-- · 2019-06-02 22:48

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.

查看更多
登录 后发表回答