使用Hibernate 3.6.0
我很难理解hibernate。我一直在使用延迟初始化异常来解决这个问题。
我有一个与RSVP有一对多关系的Event实体。当运行测试时,它会返回一个有效的事件列表。但是当我在我的控制器中进行调用以将其作为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;
}
}
MY控制器
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 :(得分:0)
您的RSVP集合是懒洋洋地获取的。 (如果未指定获取类型,则默认为惰性)。如果您计划在Hibernate会话关闭后访问它,则需要将其更改为eager:
@OneToMany(cascade = CascadeType.ALL, fetch= FetchType.EAGER)
private Set<RSVP> rsvps = new HashSet<RSVP>();