0

This is a follow up questions from this:

Spring Hibernate FetchType LazyInitializationException even when not calling association

I have tried to implement this solution but with no luck. I am wondering if I have made a mistake some where...

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>

And I am still getting the following error

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:

Please any insight or suggestions are much appreciated...

One last thing, I don't want to marke it as JsonIgnore, as it will never be serialized. There will be times where I will need to retrieve the Lazy object.

Thank you

Eric Huang
  • 1,114
  • 3
  • 22
  • 43

1 Answers1

0

to resolve this probleme there two ways. the first one is to ignore the phones if you don't need this information in this request by using @JsonIgnore. the second way is the load the phones before closing the transaction. and to do that you can use Hibernate.initialize(person.getPhones()). Or you can fetch this collection using @OneToMany(fetch = FetchType.EAGER) on the collection.