4 Replies Latest reply on Jul 14, 2011 5:09 AM by fissy101

    resteasy, jackson and LazyInitializationException

    fissy101

      Dear All,

       

      I have a JAX-RS & JPA application that worked fine under AS6 but now produces HTTP 500 errors when requesting JSON responses. I am trying to serialize the following class:

       

       

      {code}

      @Entity

      @Table(name="countries")

      @XmlRootElement(name="Country")

      @XmlType(name="Country",propOrder={"name"})

      @JsonAutoDetect({JsonMethod.NONE})

      public class Country implements Serializable {

       

          private static final long serialVersionUID = 4519728176152383566L;

         

          private Integer id;

          private Country parent;

          private String name;

         

          @Id

          @GeneratedValue

          @Column(name="country_id")

          @XmlAttribute(name="id",required=true)

          @JsonProperty

          public Integer getId() {return id;}

          public void setId(Integer id) {this.id = id;}

         

          @ManyToOne(fetch=FetchType.LAZY)

          @JoinColumn(name="parent_id",referencedColumnName="country_id")

          @XmlTransient

          @JsonIgnore

          public Country getParent() {return parent;}

          public void setParent(Country parent) {this.parent = parent;}

         

          @Column(name="name")

          @XmlElement(name="Name",required=true)

          @JsonProperty

          public String getName() {return name;}

          public void setName(String name) {this.name = name;}

      }

      {code}

       

      The @Json annotations forced jackson to only include the correct properties. Under AS7, these annotations no longer seem to be having any effect, resulting in:

       

      org.codehaus.jackson.map.JsonMappingException caused by ... org.hibernate.LazyInitializationException

       

      This happens because jackson tries to serialize the 'parent' property of the Country class, which has not been eagerly fetched by the JPA query. I have tried several variations of the annotations but cannot find anything that works. Does anyone know the correct mix?