[英]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.