0

I have an entity question and an entity test with ManyToMany relationship between the two entitie. when I want to view the questions of a test this error is occurred.

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

this is the JPA implementation of the two entities. Question:

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

Test:

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

}
}
K.Nicholas
  • 8,797
  • 4
  • 34
  • 53
Georgio
  • 55
  • 1
  • 6
  • You should post the code qhere you perform the query to get the Test. Because thats the part you have to midify and tell your db-provider to fetch all questions. – OH GOD SPIDERS May 11 '16 at 13:41
  • it's a simple select query. test.getQuestions – Georgio May 11 '16 at 13:46
  • No, i didn't mean how you call test.getQuestions(). I meant the java code you use to load the Test object from database. In that query you can specify that all the questions should be fetched. But how depends on whether you use CriteriaApi, HQL or something different. – OH GOD SPIDERS May 11 '16 at 13:50
  • i load it from Bindings – Georgio May 17 '16 at 11:39

1 Answers1

1

When you get Test and access Test.questions later, after leaving the transaction, you will get a lazy initialization exception when the Test entity is detached and questions has not been initialized/fetched yet. You can either do a specific fetch, or depending on your configuration, you can call Test.getQuestions().size() before you exit the transaction.

References: How to solve lazy initialization exception using JPA and Hibernate as provider

LazyInitializationException in JPA and Hibernate

Hibernate LazyInitializationException using Spring CrudRepository

Community
  • 1
  • 1
K.Nicholas
  • 8,797
  • 4
  • 34
  • 53
  • i'm calling the questions of a test by Bindings so i can't do this? have you an idea is that a question of fetch or something else? – Georgio May 17 '16 at 11:39