"lazily" error in java web application

Asked

Viewed 36 times

0

Good afternoon, I am developing a library web system, however, on the book page I can usually persist with the data, but when I try to edit them, the error occurs: "HTTP Status 500 - failed to lazily initialize a Collection, could not initialize proxy - no Session", I don’t know if it’s something related to class mapping, or error on my pc. Would someone please know how to solve the problem?

I think maybe it could be the relationship of my Book class with the Author, since in the bank is automatically persisted in a table the keys of the two, but do not know how to solve.

Book Class:

@Entity
@Table(name = "livro")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Livro.findAll", query = "SELECT l FROM Livro l")
, @NamedQuery(name = "Livro.findById", query = "SELECT l FROM Livro l WHERE 
l.id = :id")
, @NamedQuery(name = "Livro.findByAno", query = "SELECT l FROM Livro l WHERE 
l.ano = :ano")
, @NamedQuery(name = "Livro.findByEdicao", query = "SELECT l FROM Livro l 
WHERE l.edicao = :edicao")
, @NamedQuery(name = "Livro.findByIsbn", query = "SELECT l FROM Livro l 
WHERE l.isbn = :isbn")
, @NamedQuery(name = "Livro.findByTitulo", query = "SELECT l FROM Livro l 
WHERE l.titulo = :titulo")})

public class Livro implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "ano")
@Temporal(TemporalType.TIMESTAMP)
private Date ano;
@Column(name = "edicao")
private Integer edicao;
@Size(max = 30)
@Column(name = "isbn")
private String isbn;
@Size(max = 50)
@Column(name = "titulo")
private String titulo;
@ManyToMany(cascade = {
    CascadeType.REFRESH})
@JoinTable(name = "AutorLivro",
        joinColumns = @JoinColumn(name = "idLivro"),
        inverseJoinColumns = @JoinColumn(name = "idAutor")
)
private List<Autor> autorList;
@JoinColumn(name = "Assunto_id", referencedColumnName = "id")
@ManyToOne
private Assunto assuntoid;
@JoinColumn(name = "Editora_id", referencedColumnName = "id")
@ManyToOne
private Editora editoraid;
@OneToMany(mappedBy = "idLivro")
private List<Exemplar> exemplarList;

public Livro() {
}

public Livro(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

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

public Date getAno() {
    return ano;
}

public void setAno(Date ano) {
    this.ano = ano;
}

public Integer getEdicao() {
    return edicao;
}

public void setEdicao(Integer edicao) {
    this.edicao = edicao;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

@XmlTransient
public List<Autor> getAutorList() {
    return autorList;
}

public void setAutorList(List<Autor> autorList) {
    this.autorList = autorList;
}

public Assunto getAssuntoid() {
    return assuntoid;
}

public void setAssuntoid(Assunto assuntoid) {
    this.assuntoid = assuntoid;
}

public Editora getEditoraid() {
    return editoraid;
}

public void setEditoraid(Editora editoraid) {
    this.editoraid = editoraid;
}

@XmlTransient
public List<Exemplar> getExemplarList() {
    return exemplarList;
}

public void setExemplarList(List<Exemplar> exemplarList) {
    this.exemplarList = exemplarList;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Livro)) {
        return false;
    }
    Livro other = (Livro) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "br.cesjf.lpwsd.model.Livro[ id=" + id + " ]";
}

}

Author Class:

@Entity
@Table(name = "autor")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Autor.findAll", query = "SELECT a FROM Autor a")
, @NamedQuery(name = "Autor.findById", query = "SELECT a FROM Autor a WHERE 
a.id = :id")
, @NamedQuery(name = "Autor.findByNome", query = "SELECT a FROM Autor a 
WHERE a.nome = :nome")})
public class Autor implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 30)
@Column(name = "nome")
private String nome;
@ManyToMany(mappedBy = "autorList")
private List<Livro> livroList;

public Autor() {
}

public Autor(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

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

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

@XmlTransient
public List<Livro> getLivroList() {
    return livroList;
}

public void setLivroList(List<Livro> livroList) {
    this.livroList = livroList;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are 
not set
    if (!(object instanceof Autor)) {
        return false;
    }
    Autor other = (Autor) object;
    if ((this.id == null && other.id != null) || (this.id != null && 
!this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "br.cesjf.lpwsd.model.Autor[ id=" + id + " ]";
}

}
  • I managed to solve it myself, I had to use a converter so that an Arraylist used on the book page would work. Anyway, thank you!

1 answer

1


I believe this error is occurring because the ORM cannot boot from the abstraction. Try to initialize variables from your lists this way:

@JoinTable(name = "AutorLivro",
        joinColumns = @JoinColumn(name = "idLivro"),
        inverseJoinColumns = @JoinColumn(name = "idAutor")
)
private List<Autor> autorList = new ArrayList();
@ManyToMany(mappedBy = "autorList")
private List<Livro> livroList = new ArrayList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.