2015-11-03 122 views
0

我有以下的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有所不同嗎?

謝謝!

+0

你能告訴我們你的AuditingEntityListener.class嗎? –

+1

這是由Spring提供的。 http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/support/AuditingEntityListener.html – user152468

回答

-1

不,不應該。

在你的代碼只設置property領域:

... 
entityWithAuditingDates.setProperty("foooo"); 
entityWithAuditingDatesRepository.save(entityWithAuditingDates); 
... 

createdDatelastModified以前沒有保存修改。

因爲這個條件不滿足:

assertNotEquals(entityWithAuditingDates.getCreatedDate(), entityWithAuditingDates.getLastModified()); 
+0

AuditingEntityListener應爲我設置這些字段。它的確如此,但並不像預期的那樣。 – user152468

-1

如果更新操作後,從數據庫中檢索實體,字段設置是否正確。下面的測試用例通過。不過,我想知道他們爲什麼在第一次保存操作時設置正確,但是在第二次保存時錯誤地設置了。當你從數據庫中檢索記錄時,你最終只能得到正確的信息。我想這與休眠緩存有關。

@Test 
public void test() throws InterruptedException { 
    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"); 
    Thread.sleep(1000); 
    entityWithAuditingDatesRepository.save(entityWithAuditingDates); 
    EntityWithAuditingDates retrieved = entityWithAuditingDatesRepository.findOne(entityWithAuditingDates.getId()); 
    assertNotNull(retrieved.getCreatedDate()); 
    assertNotNull(retrieved.getLastModified()); 
    assertNotEquals(retrieved.getCreatedDate(), retrieved.getLastModified()); 
} 
+0

請參閱文檔[here](http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#save-S-)。您不需要進行其他查詢,只需使用'save'方法返回的對象。 – Mark

5

我面臨同樣的問題,但現在想出了一個解決方法。在@Column上,我已經設置updatable = false來排除update上的create *字段。

@CreatedBy 
    @NotNull 
    @Column(name = "created_by", nullable = false, length = 50, updatable = false) 
    private String createdBy; 

    @CreatedDate 
    @NotNull 
    @Column(name = "created_date", nullable = false, updatable = false) 
    private ZonedDateTime createdDate = ZonedDateTime.now(); 

    @LastModifiedBy 
    @Column(name = "last_modified_by", length = 50) 
    private String lastModifiedBy; 

    @LastModifiedDate 
    @Column(name = "last_modified_date") 
    private ZonedDateTime lastModifiedDate = ZonedDateTime.now(); 
+0

有趣的解決方法。同時我已將更新功能寫入Service類。但我會考慮未來。 – user152468

0

沒有必要做另一個查詢來查看更新的字段。存儲庫的save方法返回一個對象,the documentation表示您應該始終用於進一步的操作。返回的對象應該傳遞最後的斷言。試試這個:

entityWithAuditingDates = entityWithAuditingDatesRepository.save(entityWithAuditingDates);