2016-06-18 38 views
0

如何使用@LastModifiedBy批註延遲加載多對一列?Lazy @LastModifiedBy

比如我有createdBy和lastModifiedBy列:

@ManyToOne(fetch = FetchType.LAZY, optional = false) 
@JoinColumn(name = "manager_id") 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
@JsonIdentityReference(alwaysAsId = true) 
private User manager; 

@ManyToOne(fetch = FetchType.LAZY, optional = false) 
@CreatedBy 
@JoinColumn(name = "created_by") 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
@JsonIdentityReference(alwaysAsId = true) 
@JsonProperty("created_user_id") 
private User createdBy; 

@ManyToOne(fetch = FetchType.EAGER, optional = false) 
@LastModifiedBy 
@JoinColumn(name = "last_modified_by") 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
@JsonIdentityReference(alwaysAsId = true) 
@JsonProperty("last_modified_user_id") 
private User lastModifiedBy; 

這是工作。但只要我嘗試延遲加載列lastModifiedBy(取= FetchType.LAZY,可選=假的)我有錯誤:

2016-06-18 23:10:36,399 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-8) Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Problem accessing property 'id': null (through reference chain: com.smcontact.cms.model.project.Project["manager"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Problem accessing property 'id': null (through reference chain: com.smcontact.cms.model.project.Project["manager"]) 

如何解決這個問題?

回答

0

解決方案:註冊休眠模塊。

Hibernate5Module hibernateModule = new Hibernate5Module(); 
objectMapper.registerModule(hibernateModule);