JPA + HIBERNATE error

0

Gentlemen. 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 'QueryTextHtml', follow the code of it.

@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 you can see, the User class has a oneToMany relationship with the class QueryHtml. And the QueryHtml class has a ManyToOne relationship with the User class. In other words, we have a two-way relationship, and the key is in the QueryHtml class. I'm trying to save a new html query using the following code:

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 it generates an error on 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 can be?

    
asked by anonymous 06.05.2018 / 17:19

1 answer

0

You need to understand the loading of lists by JPA, you can have a better idea this link

You need to add fetch so that the list is fully loaded

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

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

Source: link

    
07.05.2018 / 15:55