简体   繁体   中英

Hibernate: LazyInitializationException: failed to lazily initialize a collection of role

Using Hibernate 3.6.0

I'm having a real hard time understanding hibernate. I keep running into this issue with lazy initialization exception.

I have an Event entity with a one-to-many relationship with RSVP. When a run a test get back a list of events it works. But when I'm making the call in my controller in order to return it as json, I run into this lazy init error.

This is my event class

@Entity
@Table(name = "EVENT")
public class Event implements Serializable {

    @SequenceGenerator(name="event", sequenceName="EVENT_PK_SEQ")
    @GeneratedValue(generator="event",strategy=GenerationType.SEQUENCE)
    @Id
    @Column(name = "EVENT_ID")
    private int id;
    @Column(name = "DATE_TIME")
    private Date date;
    @Column(name = "EVENT_NAME")
    private String name;
    @Column(name = "EVENT_PARTICIPANT_LIMIT")
    private int limit;
    @Column(name = "EVENT_VISIBILITY")
    private boolean visibilty;
    @Column(name = "EVENT_LOCATION")
    private String location;
    @Column(name = "EVENT_DESCRIPTION")
    private String description;

    @OneToOne(cascade=CascadeType.REMOVE, fetch= FetchType.EAGER)
    private User author;


    private Date create_date;

    @OneToOne(cascade=CascadeType.REMOVE, fetch= FetchType.EAGER)
    private  EventType eventType;
    @OneToOne(cascade=CascadeType.REMOVE, fetch= FetchType.EAGER)
    private  EventClass eventClass;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<RSVP> rsvps = new HashSet<RSVP>();  


    @ManyToMany(mappedBy="event")
    private Set<Group> groups = new HashSet<Group>();

    public Event(int id, Date date, String name, int limit, boolean visibilty, String location, String description,
            User author, Date create_date, EventType eventType, EventClass eventClass) {
        super();
        this.id = id;
        this.date = date;
        this.name = name;
        this.limit = limit;
        this.visibilty = visibilty;
        this.location = location;
        this.description = description;
        this.author = author;
        this.create_date = create_date;
        this.eventType = eventType;
        this.eventClass = eventClass;
    }


    public Event(){
        super();
    }

    @Override
    public String toString() {
        return "Event [id=" + id + ", date=" + date + ", name=" + name + ", limit=" + limit + ", visibilty=" + visibilty
                + ", location=" + location + ", description=" + description + ", author=" + author + ", create_date="
                + create_date + ", eventType=" + eventType + ", eventClass=" + eventClass + ", rsvps=" + rsvps
                + ", groups=" + groups + "]";
    }


    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }
    public int getId() {
        return id;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }

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

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public boolean isVisibilty() {
        return visibilty;
    }

    public void setVisibilty(boolean visibilty) {
        this.visibilty = visibilty;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreate_date() {
        return create_date;
    }

    public void setCreate_date(Date create_date) {
        this.create_date = create_date;
    }

    public EventType getEventType() {
        return eventType;
    }

    public void setEventType(EventType eventType) {
        this.eventType = eventType;
    }

    public EventClass getEventClass() {
        return eventClass;
    }

    public void setEventClass(EventClass eventClass) {
        this.eventClass = eventClass;
    }


    public Set<RSVP> getRsvps() {
        return rsvps;
    }


    public void setRsvps(Set<RSVP> rsvps) {
        this.rsvps = rsvps;
    }


    public Set<Group> getGroups() {
        return groups;
    }


    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }   

}

My RSVP

@Entity
@Table(name="RSVP")
public class RSVP {

    @Id
    @Column(name="RSVP_ID")
    @SequenceGenerator(name="rsvp", sequenceName="RSVP_PK_SEQ")
    @GeneratedValue(generator="rsvp",strategy=GenerationType.SEQUENCE)
    private int rsvpId;

    @OneToOne(cascade=CascadeType.REMOVE, fetch= FetchType.EAGER)
    @JoinColumn(name="STATUS_ID")
    private RSVPStatus status;

    @ManyToOne(cascade=CascadeType.REMOVE)
    @JoinColumn(name="USER_ID")
    private User user;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="EVENT_ID")
    private Event event;



    public RSVP(int rsvpId, RSVPStatus status, User user, Event event) {
        super();
        this.rsvpId = rsvpId;
        this.status = status;
        this.user = user;
        this.event = event;
    }

    public RSVP() {
    }

    @Override
    public String toString() {
        return "RSVP [rsvpId=" + rsvpId + ", status=" + status + ", user=" + user + ", event=" + event + "]";
    }

    public int getRsvpId() {
        return rsvpId;
    }

    public void setRsvpId(int rsvpId) {
        this.rsvpId = rsvpId;
    }

    public RSVPStatus getStatus() {
        return status;
    }

    public void setStatus(RSVPStatus status) {
        this.status = status;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }   
}

MY controller

public class MyController {

    private static SessionFactory sf = HibernateUtils.getSessionFactory();
    private DataFacade df = new DataFacade(sf);

    @RequestMapping(value="home", method=RequestMethod.GET,
            consumes= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<List<Event>> getUserCal(){
        DataFacade df = new DataFacade(sf);     
        List<Event> events= df.getAllEventsByAuthor(1);
        for(Event e:events){
            System.out.println(e);
        }
        return new ResponseEntity<List<Event>>(events,HttpStatus.OK);
    }
}

Your RSVP collection is fetched lazily. (If you don't specify a fetch type, the default is lazy). You need to change it to eager if you are planning to access it after the Hibernate session is closed:

@OneToMany(cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private Set<RSVP> rsvps = new HashSet<RSVP>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM