2017-06-19 70 views
0

這是這是一個後續的問題:傑克遜序列中的懶惰對象HttpMessageNotWritableException&LazyInitializationException中

Spring Hibernate FetchType LazyInitializationException even when not calling association

我試圖實施該解決方案,但沒有運氣。我想知道如果我犯了一個錯誤,其中的一些...

Avoid Jackson serialization on non fetched lazy objects

的applicationContext.xml

<context:annotation-config /> 
<context:component-scan base-package="com.app"></context:component-scan> 

<tx:annotation-driven /> 

<mvc:annotation-driven> 
    <mvc:message-converters> 
     <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
      <property name="objectMapper"> 
       <bean class="com.app.service.HibernateAwareObjectMapper" /> 
      </property> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

<!-- Hibernate server settings --> 

HibernateAwareObjectMapper

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; 

public class HibernateAwareObjectMapper extends ObjectMapper { 

    public HibernateAwareObjectMapper() { 
     Hibernate4Module hm = new Hibernate4Module(); 
     registerModule(hm); 
    } 
} 

的pom.xml

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    <version>4.3.11.Final</version> 
</dependency> 
<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>4.3.2.Final</version> 
</dependency> 

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
    <version>2.8.6</version> 
</dependency> 

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-annotations</artifactId> 
    <version>2.8.6</version> 
</dependency> 

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.8.6</version> 
</dependency> 

<dependency> 
    <groupId>com.fasterxml.jackson.datatype</groupId> 
    <artifactId>jackson-datatype-hibernate4</artifactId> 
    <version>2.8.6</version> 

</dependency> 

,我仍然收到以下錯誤

Hibernate: select this_.person_id as person_i1_1_0_, this_.age as age2_1_0_, this_.name as name3_1_0_ from person this_ 
[Person [person_id=1, name=eric, age=11]] 
Jun 20, 2017 12:37:29 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotWritable 
WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.app.person.Person.phones, could not initialize proxy - no Session (through reference chain: java.util.HashMap["results"]->java.util.ArrayList[0]->com.app.person.Person["phones"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 

請任何見解或建議,我們非常感激......

最後一兩件事,我不想坊間它作爲JsonIgnore,因爲它永遠不會被序列化。有時我需要檢索Lazy對象。

謝謝

回答

0

解決這個問題有兩種方法。第一個是如果您通過使用@JsonIgnore不需要此信息,則忽略電話。第二種方式是在關閉交易之前加載手機。要做到這一點,你可以使用Hibernate.initialize(person.getPhones())。或者您可以在集合上使用@OneToMany(fetch = FetchType.EAGER)獲取此集合。

+0

不能做Eager,因爲我不想讓手機一直提取。無法執行@JsonIgnore,因爲我將需要檢索延遲對象。不需要電話對象所有的時間這就是爲什麼我使用懶惰....感謝您嘗試 –

+0

你可以使用Hibernate.initialize(person.getPhones())當你需要加載手機。 –

+0

我不想加載電話只是人 –