JPA+HIBERNATE error

Asked

Viewed 208 times

0

Sirs. I have a class called User.

@Entity
@Table(name = "tb_usuario")
public class Usuario {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "email", nullable = false, unique = true)
private String email;

@Column(name = "senha", nullable = false, length = 32)
private String senha;

@Column(name = "hash_md5", nullable = false, length = 32)
private String hashMD5Email;

@Column(name = "status", nullable = false)
private int ativo = 0;

@OneToMany(mappedBy = "usuario",cascade = CascadeType.ALL)
//@JoinColumn(name = "id_usuario")
private List consultas ;

@Column(name = "data_cadastro", nullable = false)
@Temporal(TemporalType.DATE)
private Calendar dataCadastro;

public Usuario() {

}


public void setDataCadastro(Calendar dataCadastro) {
    this.dataCadastro = dataCadastro;
}

public Calendar getDataCadastro() {
    return dataCadastro;
}

public int getId() {
    return id;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getSenha() {
    return senha;
}

public void setSenha(String senha) {
    this.senha = senha;
}

public void setAtivo(int ativo) {
    this.ativo = ativo;
}

public int getAtivo() {
    return ativo;
}


public List getConsultas() {
    return consultas;
}


public void setConsultas(List consultas) {
    this.consultas = consultas;
}

I also have a class called Consultatratadahtml, follows the code of the same.

@Entity
@Table(name = "tb_consulta")
public class ConsultaTratadaHtml {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "consulta_html", nullable = false, length = 500)
private String consultaHtml;

@Column(name = "data_consulta", nullable = false)
@Temporal(TemporalType.DATE)
private Calendar dataConsulta;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="id_usuario",nullable = false)
private Usuario usuario;


public int getId() {
    return id;
}

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

public String getConsultaHtml() {
    return consultaHtml;
}

public void setConsultaHtml(String consultaHtml) {
    this.consultaHtml = consultaHtml;
}

public Calendar getDataConsulta() {
    return dataConsulta;
}

public void setDataConsulta(Calendar dataConsulta) {
    this.dataConsulta = dataConsulta;
}

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}
}

As it is possible to notice the class User has a relationship oneToMany with the class Consulttratadahtml. And the Consultatratadahtml class has a Manytoone relationship with the User class. In other words, we have a bidirectional relationship, and the key is in the class Consultatratadahtml. I am trying to save a new Query :

Usuario usuario = new UsuarioDAO().buscarUsuarioByEmail("pedro@gmail");

    ConsultaTratadaHtml consulta = new ConsultaTratadaHtml();

    consulta.setConsultaHtml("teste de persistencia");
    consulta.setDataConsulta(Calendar.getInstance());               

    consulta.setUsuario(usuario);
    usuario.getConsultas().add(consulta);

    new ConsultaTratadaHtmlDAO().salvarConsulta(consulta);

But generates an error in the following line :

usuario.getConsultas().add(consulta);

The following error message

Exception in thread “main” org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:     br.com.pedrodev.webservicecpf.domain.Usuario.consultas, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:392)
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:297)
at testes.Programa.main(Programa.java:39)

Does anyone have any idea what it might be?

1 answer

0


You need to understand list loading by JPA, you might have a better idea at this link

You need to add the fetch for the list to be fully loaded

@OneToMany(mappedBy = "usuario",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List consultas ;

Full loading of lists can generate memory problems, so you should be careful when using. Another alternative is to activate the transaction proxy, if using Spring simply add @Transactional on the method, which it will enable the proxy

Source:https://stackoverflow.com/a/22822283/4395789

Browser other questions tagged

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