Galera, estou com problema para salvar um objeto no Banco de Dados.
Está dando LazyInitializationException.
Eu tenho a entity TipoArquivo e nela eu tenho o mapeamento OneToMany para arquivo.
@Entity
@Table(name="tbl_prd_tipo_arquivo")
public class TipoArquivo implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id_tipo_arquivo")
private Integer idTipoArquivo;
@Column(name="tipo_arquivo", nullable=true)
private String tipoArquivo;
@OneToMany(mappedBy="tipoArquivo", fetch=FetchType.LAZY)
private List<Arquivo> arquivoList;
A Entity Arquivo possui uma fk de TipoArquivo.
@Entity
@Table(name="tbl_prd_arquivo")
public class Arquivo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_arquivo")
private Integer idArquivo;
@ManyToOne
@JoinColumn(name="id_tipo_arquivo_fk", referencedColumnName="id_tipo_arquivo")
private TipoArquivo tipoArquivo;
Estou usando este método genérico para salvar um objeto no Banco.
[code]
public final void salvar(Object objeto) {
EntityManager em = getEntityManager();
try {
em.persist(objeto);
em.getTransaction().commit();
} catch (RuntimeException e) {
em.getTransaction().rollback();
throw e;
} finally {
em.close();
}
}[/code]
Então, me dá essa Exception:
[color=red]SEVERE: Servlet.service() for servlet default threw exception
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.syncrobot.repositoriodigital.entity.TipoArquivo.arquivoList, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.toString(PersistentBag.java:483)
at java.lang.String.valueOf(String.java:2826)[/color]
Alguém já passou por esse problema?