Spring Jpa這項技術在Spring 開發中經常用到。
今天在做項目用到了Entity的關聯懶加載,但是在返回Json的時候,不管關聯數據有沒有被加載,都會觸發數據序列化,而如果關聯關系沒有被加載,此時是一個
HibernateProxy
,並不是真實的數據,而導致了報錯。
例如這個Topic Entity:
@Entity
@Table(name = "yms_topics")
@Getter
@Setter
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NamedEntityGraphs({
@NamedEntityGraph(name = "topic.all",
attributeNodes = {
@NamedAttributeNode(value = "author"),
@NamedAttributeNode(value = "category")
})
})
public class Topic implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User author;
@ManyToOne(targetEntity = TopicCategory.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private TopicCategory category;
@Column(nullable = false, length = 200)
private String title;
@Lob
@Column(nullable = false, length = 50000)
private String content;
@CreatedDate
private Date createdAt;
@LastModifiedDate
private Date updatedAt;
}
author 和 category 都是多對一的關聯,也就是作者和分類,定義的是懶加載LAZY
,現在需要分頁取出記錄,Repository 如下:
@EntityGraph(value = "topic.all")
Page<Topic> findAll(Pageable pageable);
這是關聯讀取author和category數據,沒有任何問題。但是如果有的關聯不需要加載,將EntityGraph去掉,就會報錯。
Page<Topic> findAll(Pageable pageable);
究其原因就是HibernateProxy 沒有辦法被序列化,網上有很多的方法,例如JsonIgnoreProperties
,這是治標不治本的方法
現在要達到的目標是當有關聯數據的時候序列化,不存在的時候不返回,或者直接返回Null。
其實要解決這個問題很簡單,那就是使用 Jackson 的一個包 jackson-datatype-hibernate5
。
首先gradle添加依賴:
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.9.8'
這個版本要注意jackson-datatype-hibernateX
,根據Hibernate的版本來定
然后我們要重寫 SpringMvc的 MappingJackson2HttpMessageConverter
,將Hibernate5Module
這個Module 注冊到ObjectMapper
。
我們新建一個WebMvcConfig類,如下:
@Configuration
public class WebMvcConfig {
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = converter.getObjectMapper();
Hibernate5Module hibernate5Module = new Hibernate5Module();
mapper.registerModule(hibernate5Module);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return converter;
}
}
這是一個Config類,很簡單
- 就是注入一個Bean,類型為
MappingJackson2HttpMessageConverter
,獲取到ObjectMapper - 通過
mapper.registerModule(hibernate5Module);
注冊Module - 還可以定義時間如期的序列化格式。
- 注意如果要讓未加載的時候完全不輸出,那么在Entity的類級別注解要使用Empty,例如:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
,不然當數據為null的時候會輸出null。
到這里我們就可以達到預期的目的了。
這里可能會導致spring.jackson的配置失效,以后再行研究。