Errai, Hibernate and JSON lazyInitializationException
twinj Jun 11, 2012 5:09 AMI have done some sampling work on the kitchen sink quickstart app and have had quite the run with playing with the REST services.
All of the services I have created work well when using XML. However, when fetching entities with JSON as the requested content type I get this error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role no session or session was closed.
The entities have several OneToMany relationships and are defaulted to a Lazy fetch. If I change the bag non persistant properties to an eager fetch I get a non stop loop of data.
I have searched for a solution but cannot find one which is suitable for errai: I have seen a solution where I can add @JsonIgnore on the methods that do not corelate to the entities persistant fields but I think this was from the Jackson API. It is my understanding that Errai has its own JSON engine?
Are there any suggestions on how this could be remedied using Errai JSON or any other suitable workaround that someone knows of?
@Portable @Entity @XmlRootElement public class Territory implements Serializable { @Id private Integer id; ... private String name; @OneToMany(fetch=FetchType.LAZY, mappedBy = "territory") private List<Merchandiser> merchandisers = new ArrayList<Merchandiser>(); @OneToMany(fetch=FetchType.LAZY, mappedBy = "territory") private List<Store> stores = new ArrayList<Store>(); ... @XmlTransient public List<Merchandiser> getMerchandisers() { return merchandisers; } ...
@Portable @Entity @XmlRootElement public class Merchandiser implements Serializable, Comparable<Merchandiser> { @Id private Integer id; ... private String name; ... private String mobile; ... private String email; @JoinColumn(name = "address", referencedColumnName = "id") @ManyToOne(fetch=FetchType.EAGER) private Address address; @JoinColumn(name = "territory", referencedColumnName = "id") @ManyToOne(fetch=FetchType.EAGER) private Territory territory; ...
Addendum:
I am assuming this relates to this: http://jira.codehaus.org/browse/JACKSON-276
Message was edited by: Daniel Kemp