gpt4 book ai didi

java - 未能延迟初始化角色多对多关系的集合

转载 作者:行者123 更新时间:2023-12-01 09:56:33 27 4
gpt4 key购买 nike

我有一个实体问题和一个实体测试,两个实体之间具有多对多关系。当我想查看测试的问题时,出现此错误。

failed to lazily initialize a collection of role:     
tn.esen.entities.Test.questions, could not initialize proxy - no Session

这是两个实体的 JPA 实现。问题:

 package tn.esen.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
@Entity
public class Question implements Serializable {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((contenu == null) ? 0 :
contenu.hashCode());
result = prime * result + ((dateCreation == null) ? 0 :
dateCreation.hashCode());
result = prime * result + ((niveauDeDifficulte == null) ? 0 :
niveauDeDifficulte.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Question other = (Question) obj;
if (contenu == null) {
if (other.contenu != null)
return false;
} else if (!contenu.equals(other.contenu))
return false;
if (dateCreation == null) {
if (other.dateCreation != null)
return false;
} else if (!dateCreation.equals(other.dateCreation))
return false;
if (niveauDeDifficulte == null) {
if (other.niveauDeDifficulte != null)
return false;
} else if (!niveauDeDifficulte.equals(other.niveauDeDifficulte))
return false;
return true;
}

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_question")
private int id;
private String contenu;
private String niveauDeDifficulte;
private Date dateCreation;
@OneToMany(mappedBy="question",fetch = FetchType.EAGER)
private Collection <Reponse> reponses;
@ManyToOne
private Categorie categorie;
@ManyToOne
private Administrateur administrateur;
@ManyToMany(mappedBy = "questions",fetch = FetchType.EAGER)
private Collection<Test> tests;
public Question(){
super();
}

@Override
public String toString() {
return "Question [id=" + id + ", contenu=" + contenu + ",
niveauDeDifficulte=" + niveauDeDifficulte + "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getContenu() {
return contenu;
}

public void setContenu(String contenu) {
this.contenu = contenu;
}

public String getNiveauDeDifficulte() {
return niveauDeDifficulte;
}

public void setNiveauDeDifficulte(String niveauDeDifficulte) {
this.niveauDeDifficulte = niveauDeDifficulte;
}


public Date getDateCreation() {
return dateCreation;
}

public void setDateCreation(Date dateCreation) {
this.dateCreation = dateCreation;
}


public Collection<Reponse> getReponses() {
return reponses;
}

public void setReponses(Collection<Reponse> reponses) {
this.reponses = reponses;
}

public Categorie getCategorie() {
return categorie;
}

public void setCategorie(Categorie categorie) {
this.categorie = categorie;
}

public Administrateur getAdministrateur() {
return administrateur;
}

public void setAdministrateur(Administrateur administrateur) {
this.administrateur = administrateur;
}

public Collection<Test> getTest() {
return tests;
}

public void setTest(Collection<Test> tests) {
this.tests = tests;
}

public Question(String contenu, String niveauDeDifficulte, Date
dateCreation) {
super();
this.contenu = contenu;
this.niveauDeDifficulte = niveauDeDifficulte;
this.dateCreation = dateCreation;
}

测试:

   package tn.esen.entities;


@Entity
public class Test implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String duree;
private String typeDePreparation;
private String lieu;
private int nbrQuestionFacile;
private int nbrQuestionMoyen;
private int nbrQuestionDifficle;
@OneToMany(mappedBy = "test")
private Collection<Resultat> resultats;
@ManyToMany
private Collection<Question> questions;

@ManyToOne
private ResponsableTechnique responsableTechnique;

public Test() {
super();
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}



public String getDuree() {
return duree;
}

public void setDuree(String duree) {
this.duree = duree;
}

public Collection<Question> getQuestions() {
return questions;
}

public void setQuestions(Collection<Question> questions) {
this.questions = questions;
}

public Test(String duree, String typeDePreparation, String lieu, int
nbrQuestionFacile, int nbrQuestionMoyen,
int nbrQuestionDifficle) {
super();
this.duree = duree;
this.typeDePreparation = typeDePreparation;
this.lieu = lieu;
this.nbrQuestionFacile = nbrQuestionFacile;
this.nbrQuestionMoyen = nbrQuestionMoyen;
this.nbrQuestionDifficle = nbrQuestionDifficle;
}

public ResponsableTechnique getRésponsableTechnique() {
return responsableTechnique;
}

public void setRésponsableTechnique(ResponsableTechnique
résponsableTechnique) {
this.responsableTechnique = résponsableTechnique;
}

public String getTypeDePreparation() {
return typeDePreparation;
}

public void setTypeDePreparation(String typeDePreparation) {
this.typeDePreparation = typeDePreparation;
}

public String getLieu() {
return lieu;
}

public void setLieu(String lieu) {
this.lieu = lieu;
}

public int getNbrQuestionFacile() {
return nbrQuestionFacile;
}

public void setNbrQuestionFacile(int nbrQuestionFacile) {
this.nbrQuestionFacile = nbrQuestionFacile;
}

public int getNbrQuestionMoyen() {
return nbrQuestionMoyen;
}

public void setNbrQuestionMoyen(int nbrQuestionMoyen) {
this.nbrQuestionMoyen = nbrQuestionMoyen;
}

public int getNbrQuestionDifficle() {
return nbrQuestionDifficle;
}

public void setNbrQuestionDifficle(int nbrQuestionDifficle) {
this.nbrQuestionDifficle = nbrQuestionDifficle;
}

public Collection<Resultat> getResultats() {
return resultats;
}

public void setRésultats(Collection<Resultat> resultats) {
this.resultats = resultats;
}

}
}

最佳答案

当你获取Test并稍后访问Test.questions时,离开事务后,当Test实体已分离,问题尚未初始化/获取。您可以执行特定的提取,也可以根据您的配置,在退出事务之前调用 Test.getQuestions().size()

引用文献:How to solve lazy initialization exception using JPA and Hibernate as provider

LazyInitializationException in JPA and Hibernate

Hibernate LazyInitializationException using Spring CrudRepository

关于java - 未能延迟初始化角色多对多关系的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com