title: Avoid Jackson serialization on non fetched lazy objects
body: <p>I have a simple controller that return a User object, this user have a attribute coordinates that have the hibernate property FetchType.LAZY.</p>
  
  <p>When I try to get this user, I always have to load all the coordinates to get the user object, otherwise when Jackson try to serialize the User throws the exception:</p>
  
  <blockquote>
    <p>com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session</p>
  </blockquote>
  
  <p>This is due to Jackson is trying to fetch this unfetched object. Here are the objects:</p>
  
  <pre><code>public class User{
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
      @JsonManagedReference("user-coordinate")
      private List&lt;Coordinate&gt; coordinates;
  }
  
  public class Coordinate {
  
      @ManyToOne
      @JoinColumn(name = "user_id", nullable = false)
      @JsonBackReference("user-coordinate")
      private User user;
  }
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>@RequestMapping(value = "/user/{username}", method=RequestMethod.GET)
  public @ResponseBody User getUser(@PathVariable String username) {
  
      User user = userService.getUser(username);
  
      return user;
  
  }
  </code></pre>
  
  <p>There is a way to tell Jackson to not serialize the unfetched objects? I've been looking other answers posted 3 years ago implementing jackson-hibernate-module. But probably it could be achieved with a new jackson feature.</p>
  
  <p>My versions are:</p>
  
  <ul>
  <li>Spring 3.2.5 </li>
  <li>Hibernate 4.1.7 </li>
  <li>Jackson 2.2</li>
  </ul>
  
  <p>Thanks in advance.</p>