[英]Spring, Hibernate : Lazy initialization exception for many-to-many mapping
我正在开发一个Spring-MVC应用程序,其中我试图为两个实体之间已经存在的一对多映射添加多对多映射。 我们在项目中拥有的两个实体是GroupSection
和GroupNotes
。
对于项目中的一项任务,我不得不在GroupSection和GroupNotes之间引入多对多映射,但是我遇到了一个懒惰的初始化异常。
错误:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.tooltank.spring.model.GroupSection.groupSections, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.tooltank.spring.model.GroupSection["groupSections"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.tooltank.spring.model.GroupSection.groupSections, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.tooltank.spring.model.GroupSection["groupSections"])
这是被称为的控制器方法:
@RequestMapping(value = "/sections/get/{canvasid}")
public @ResponseBody List<GroupSection> getAllSectionsForCanvas(@PathVariable("canvasid") int canvasid) {
boolean value = this.personService.getCanvasValuesForCanvas(canvasid);
return this.groupSectionService.listGroupSectionByCanvasid(canvasid, value);
}
DAO方法(Service方法仅调用DAO方法):
@Override
@Transactional(readOnly = true)
public List<GroupSection> listGroupSectionByCanvasid(int mcanvasid) {
try {
Session session = this.sessionFactory.getCurrentSession();
org.hibernate.Query query = session.createQuery("From GroupSection as msection where " +
"msection.currentcanvas.mcanvasid=:mcanvasid and msection.sectionDisabled=false and msection.sectionInActive=false");
query.setParameter("mcanvasid", mcanvasid);
return query.list();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
GroupSection模型:
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "membersection")
public class GroupSection {
// Below is self-mapping, required for one of the task, works.
@JsonIgnore
@ManyToOne
@JoinColumn(name = "owned_section_id", nullable = true)
private GroupSection primarySection;
@OneToMany(mappedBy = "primarySection", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private Set<GroupSection> groupSections = new HashSet<>();
// Many to many mapping, I had lazy loading before, but tried Eager to see if error goes, it doesn't. :
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
@JoinTable(name = "sectionjunction",joinColumns = {@JoinColumn(name = "msectionid")},
inverseJoinColumns = {@JoinColumn(name = "mnoteid")})
private Set<GroupNotes> groupNotesSet = new HashSet<>();
// One to many mapping below :
@OneToMany(mappedBy = "ownednotes", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JsonIgnore
private Set<GroupNotes> sectionsnotes = new HashSet<>();
}
组注释:
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "groupnotes")
public class GroupNotes implements Serializable {
@JsonIgnore
@ManyToMany(mappedBy = "groupNotesSet",fetch = FetchType.EAGER)
private Set<GroupSection> groupSectionSet = new HashSet<>();
@ManyToOne
@JoinColumn(name = "msectionid",nullable = true)
@JsonIgnore
private GroupSection ownednotes;
// Self mappings :
@JsonIgnore
@ManyToOne
@JoinColumn(name = "owned_note_id", nullable = true)
private GroupNotes primaryNote;
@OneToMany(mappedBy = "primaryNote", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private Set<GroupNotes> groupNotesSet = new HashSet<>();
}
我究竟做错了什么? 是否可以同时在2个类之间进行一对多和多对多映射。 如果是,那么该错误怎么处理。 请让我知道。 谢谢。 :-)
调用listGroupSectionByCanvasid时,您打开一个事务和一个会话。 呼叫返回时,会话关闭。 当spring返回值时,它尝试读取您的对象,并且由于延迟加载了许多关系groupNotesSet和groupSections是需要会话的休眠集合。 在您的情况下,会话将不再存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.