2017-03-24 1 views
-1

의 컬렉션을 초기화하지 못했습니다. lazy 초기화 예외로이 문제를 계속 실행합니다.최대 절전 모드 : LazyInitializationException이는 : 게으르게 내가 최대 절전 모드를 이해하는 진짜 힘든 시간을 보내고 있습니다</p> <p>최대 절전 모드 3.6.0을 사용하여 역할

RSVP와 일대 다 관계가있는 Event 엔터티가 있습니다. 테스트를 실행하면 이벤트 목록이 반환됩니다. 하지만 json으로 반환하기 위해 내 컨트롤러에서 호출 할 때이 게으른 init 오류가 발생합니다.

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 수집이 느리게 페치

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

내 컨트롤러 내 이벤트 클래스입니다. 가져 오기 유형을 지정하지 않으면 기본값은 lazy입니다. Hibernate 세션이 닫힌 후에 액세스하려는 경우 열의로 변경해야합니다.

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

저에게 맞지 않습니다. 나는 게으르고 열심히 그들을 설정하려고 노력했다. 어느 쪽이든 동일한 오류 메시지가 발생했습니다 – joejoeso

+0

지연 초기화 예외를 얻는 코드 줄을 표시 할 수 있습니까? 귀하의 외관 수업을 보여주십시오. – VHS