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