1

I have two business objects having many too many relationships. I am using a REST service to call the DAO method given below and get a list of political indicators for a political event. However though the piList in DAO successfully gives me the list of Political Indicators but it still gives me an exception

Failed to lazily intialize a collection of role...

through reference chain:

org.hibernate.collection.internal.PersistentBag[0]----->PolIndicator.piList.role
org.jboss.resteasy.spi.writerException
org.codehaus.jackson.map.JsonmappingException"

I have used @JsonIgnore in the Political Indicator class against the political event property but still the lazy exception happens.

Where am I going wrong?

PolEvent {

    @Id
    @Column(name="SEQ_EVENT_ID")
    private BigDecimal id;

    @Column(name="EVENT_NAME")
    private String eventName;

    @ManyToMany
    @JoinTable(
        name="POL_LINK_INDCTR"
        joinColumns={@JoinColumn(name="SEQ_EVENT_ID")},
        inverseJoinColumns=@JoinColumn(name="SEQ_PI_ID")
    )
    private List <PolIndicator> piList;
}


PolIndicator {

    @Id
    @Column(name="SEQ_PI_ID")
    private BigDecimal id;

    @Column(name="POL_IND_NAME")
    private String piName;

    @ManyToMany(mappedBy="piList")
    @JsonIgnore
    private List <PolEvent> eventList; 
}

DAO Layer Code

public List <PolIndicator> getPiList (String eventId) {

    Criteria criteria = session.createCriteria(PolEvent.class);
    criteria.add(Restrictions.eq("id",id);
    PolEvent polEvent = new PolEvent();
    polEvent=criteria.uniqueResult();
    piList = polEvent.getPiList();
    return piList();
}
darijan
  • 9,725
  • 25
  • 38
raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • 1
    Since it is not clear how does hibernate triggers json operations full stacktrace can help – AlexR May 13 '14 at 10:27
  • What happens if you completele remove eventList? – AlexR May 13 '14 at 10:31
  • Sorry for the late reply Alex. I work in an environement where internet access is severely restricted for security constraints. I was able to solve the problem by removing @JsonIgnore. – raikumardipak May 31 '14 at 05:07

1 Answers1

3

You need to move the annotation to the getter method:

@JsonIgnore
public List <PolEvent> getEventList() {
    return eventList;
}
Steve C
  • 18,876
  • 5
  • 34
  • 37
  • 1
    When i use @JsonIgnore , that fileld is removed from my json ! any solution for this ? –  Jun 03 '15 at 19:32
  • When I use @JsonIgnore, that field is always omitted as said Romeo, any solutions to that – Arshad Ali Oct 04 '15 at 04:23
  • That's what `@JsonIgnore` is for. You should ask a new question describing the behaviour you're getting and the behaviour you want, including source code. – Steve C Oct 04 '15 at 10:28