Erro quando a classe movimentação está preenchida com os dados no banco, caso não haja nenhum registro, consigo acessar meu xhtml normalmente. Na classe conta eu corrigo com fetch = FetchType.EAGER, mas na classe movimentacao esse erro persiste, Vejam os relacionamentos:
Conta
@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
Movimentacao
@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
Categoria
@Entity
public class Categoria implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nome;
getters e setters
Erro:
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
...