2017-06-19 6 views
0

怠惰なオブジェクトをシリアル化:ジャクソンは、これは、このからのフォローアップの質問ですHttpMessageNotWritableException&LazyInitializationException

Spring Hibernate FetchType LazyInitializationException even when not calling association

私は、このソリューションを実装しようとしましたが、運としています。私は間違いいくつかの場所を作った場合、私は

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); 
    } 
} 

HibernateAwareObjectMapper applicationContext.xmlを ...のpom.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 --> 

Avoid Jackson serialization on non fetched lazy objects

を疑問に思って

<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

この問題を解決するには2つの方法があります。 @JsonIgnoreを使用してこの情報でこの情報を必要としない場合、最初のものは電話を無視することです。 2番目の方法は、トランザクションを閉じる前に電話機をロードすることです。それを行うにはHibernate.initialize(person.getPhones())を使用できます。または、コレクションに@OneToMany(fetch = FetchType.EAGER)を使用してこのコレクションを取得できます。

+0

私はいつも電話をフェッチしたくないので、Eagerはできません。遅延オブジェクトを取得する必要があるので、@ JsonIgnoreを実行できません。あなたが電話をロードする必要があるときに、私がLazyを使用する理由は常に電話オブジェクトを必要としません.... –

+0

あなたはHibernate.initialize(person.getPhones())を使用することができます。 –

+0

私は人物 –

関連する問題