0

My JPA Entity classes have a @PrePersist method to set an INSERT_TIME column:

@PrePersist
protected void onCreate() {
    insertTime = new Date(); // TODO: inject time source?
}

For testing, I would like to replace new Date() so that entities are persisted with predictable data. My first thought was to have a data provider injected into the Entity class, but I don't control the creation of Entity classes, so it doesn't seem there is a way for me to inject dependencies. Any suggestions?

kevin cline
  • 2,608
  • 2
  • 25
  • 38
  • 1
    Why not move this logic into your dao/service layer where you have injection available? Pojos are generally best kept as dumb as possible – radai Jul 24 '14 at 16:18
  • 1
    @radai At least in a Spring environment injection into pojo is simple see `@Configurable`. Imho advising people to keep the domain layer dumb is not a good advise: http://www.martinfowler.com/bliki/AnemicDomainModel.html – Andreas Jul 24 '14 at 16:46
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) and http://stackoverflow.com/questions/24839484/spring-autowired-for-pojo-that-not-managed-by-spring/ – Andreas Jul 24 '14 at 17:00

0 Answers0