我有以下的JPA實體:彈簧數據的JPA AuditingEntityListener createdDate在更新保存現有的實體
@EntityListeners(AuditingEntityListener.class)
@Entity
public class EntityWithAuditingDates {
@Id
@GeneratedValue
private Long id;
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date lastModified;
private String property;
// getters and setters omitted.
}
而下面CrudRepository:
@Service
public interface EntityWithAuditingDatesRepository extends CrudRepository<EntityWithAuditingDates, Long> {
}
而且下面的測試:
@SpringApplicationConfiguration(classes = FooApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class AuditingEntityListenerTest {
@Autowired
private EntityWithAuditingDatesRepository entityWithAuditingDatesRepository;
@Test
public void test() {
EntityWithAuditingDates entityWithAuditingDates = new EntityWithAuditingDates();
entityWithAuditingDates.setProperty("foo");
assertNull(entityWithAuditingDates.getCreatedDate());
assertNull(entityWithAuditingDates.getLastModified());
entityWithAuditingDatesRepository.save(entityWithAuditingDates);
assertNotNull(entityWithAuditingDates.getCreatedDate());
assertNotNull(entityWithAuditingDates.getLastModified());
assertEquals(entityWithAuditingDates.getLastModified(), entityWithAuditingDates.getCreatedDate());
entityWithAuditingDates.setProperty("foooo");
entityWithAuditingDatesRepository.save(entityWithAuditingDates);
assertNotEquals(entityWithAuditingDates.getCreatedDate(), entityWithAuditingDates.getLastModified());
}
}
最後一個條件失敗。在更新實體之後,不應該使createdDate和lastModifiedDate有所不同嗎?
謝謝!
你能告訴我們你的AuditingEntityListener.class嗎? –
這是由Spring提供的。 http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/support/AuditingEntityListener.html – user152468