2017-03-24 25 views
-1

的集合中使用Hibernate 3.6.0休眠:LazyInitializationException异常:未能初始化懒洋洋角色

我有一个真正的很难理解休眠。我一直用惰性初始化异常来解决这个问题。

我有一个事件实体与RSVP的一对多关系。运行测试时会返回它工作的事件列表。但是当我在我的控制器中进行调用以将其作为json返回时,我遇到了这个懒惰的初始化错误。

这是我的事件类

@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; 
    } 

} 

我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; 
    } 
} 

我的控制器

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); 
    } 
} 

回答

0

你RSVP收集获取懒洋洋地。 (如果您不指定提取类型,则默认为懒惰)。如果您打算在Hibernate会话关闭后访问它,则需要将其更改为渴望:

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

这对我而言并不适用。我试图设定他们都懒惰和渴望。无论哪种方式,我得到了同样的错误消息msg – joejoeso

+0

你可以显示你获得惰性初始化异常的代码行吗?同时显示你的门面类 – VHS

相关问题