Error org.hibernate.Lazyinitializationexception

Asked

Viewed 2,968 times

0

Error when the drive class is filled with data in the database, if there is no record, I can access my xhtml normally. In the class I count with fetch = Fetchtype.EAGER, but in the class drive this error persists, See the relationships:

Bill

@Entity
//@Cacheable
//@Table(uniqueConstraints={@UniqueConstraint(columnNames = { "agencia","numero" })})
public class Conta implements Serializable {

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

private String titular;
private String agencia;
private String numero;
private String banco;

@OneToOne
@JoinColumn(unique=true)
private Gerente gerente;

//@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@OneToMany(fetch = FetchType.EAGER, mappedBy="conta") 
private List<Movimentacao> movimentacoes;

getters e setters

Movement

@Entity
public class Movimentacao implements Serializable {

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

private String descricao;
private Calendar data;
private BigDecimal valor;

@ManyToOne
private Conta conta;

@ManyToMany
@JoinTable(name = "categorias_da_movimentacao",
        joinColumns = @JoinColumn(name="movimentacao_id"))
private List<Categoria> categorias = new ArrayList<Categoria>();

@Enumerated(EnumType.STRING)
@Column(name = "tipo_movimentacao")
private TipoMovimentacao tipoMovimentacao;

getters e setters

Category

@Entity
public class Categoria implements Serializable {

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

private String nome;

getters e setters

Error:

16:44:04,872 GRAVE [javax.enterprise.resource.webcontainer.jsf.application] (http-localhost-127.0.0.1-8080-1) Error Rendering View[/movimentacoes.xhtml]: 
javax.el.ELException: /movimentacoes.xhtml: failed to lazily initialize a collection of role: br.com.caelum.financas.modelo.Movimentacao.categorias, no session or session was closed
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:90) [jsf-impl-2.1.7-jbossorg-2.jar:]

...

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.caelum.financas.modelo.Movimentacao.categorias, 
no session or session was closed
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

...

16:44:04,924 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/fj25-financas-web].[Faces Servlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet Faces Servlet threw exception: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.caelum.financas.modelo.Movimentacao.categorias, no session or session was closed

...

2 answers

1

  • Through a consultation I would not solve this no? Some searches I did say to insert the use of JOIN FETCH and etc. Creating an init method this code you gave me would look the same way? star and end within the method?

  • Yes, you have to start the transaction, start your list and then close it.

0


Well I managed to resolve by changing my dao query, where JPQL offers an explicit way to load the relationships through Join along with the fetch clause.

public List<Movimentacao> listaComCategorias() {
    return this.manager.createQuery("select distinct m from Movimentacao m left join fetch m.categorias",
            Movimentacao.class).getResultList();
}

Browser other questions tagged

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