Hello.
I'm having difficulty running my fetch = FetchType.LAZY relationships with the @OneToMany annotation. In my entity Nota Fiscal, there are several lists fetch = FetchType.LAZY, when loading the object by id, I load all these lists with left outer join. In the first instance (debugging) all lists are loaded. However, the moment I change the tab and the request of the mailing list (listEmail) is made, I am returning the error:
org.hibernate.LazyInitializationException: failed to initialize a collection of role: br.inf.criare.erp.nota.domain.NotaFiscal.listaEmail, could not initialize proxy - no Session
To try to explain better, it follows part of the class Fiscal Note
@Entity
@Table(name = "notas_fiscais")
public class NotaFiscal implements IEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
cascade = {CascadeType.ALL},
orphanRemoval = true)
private List<NotaFiscalItem> itens;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
cascade = {CascadeType.ALL},
orphanRemoval = true)
private List<NotaFiscalDocRef> listaDocRef;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
cascade = {CascadeType.ALL},
orphanRemoval = true)
private List<NotaFiscalProcRef> listaProcRef;
//-----------------------------------------------------
//GRUPO DE CAMPO DE USO LIVRE
//-----------------------------------------------------
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
cascade = {javax.persistence.CascadeType.ALL},
orphanRemoval = true)
private List<NotaFiscalObsCont> listaObsCont;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
orphanRemoval = true,
cascade = {CascadeType.ALL})
private List<NotaFiscalVeiculo> veiculos;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
orphanRemoval = true,
cascade = {CascadeType.ALL})
private List<NotaFiscalVolumes> volumes;
@OneToMany(mappedBy = "notaFiscal", fetch = FetchType.LAZY,
cascade = {CascadeType.ALL},
orphanRemoval = true)
private List<NotaFiscalEvento> eventos;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "notas_fiscais_email", joinColumns=@JoinColumn(name="nota_fiscal_id"))
private List<NotaFiscalEmail> listaEmail;
@ElementCollection(fetch = FetchType.LAZY)
@Column(name = "documento", length = 20, nullable = false)
@CollectionTable(name = "notas_fiscais_autxml", joinColumns=@JoinColumn(name="nota_fiscal_id"))
private List<String> listaAutXml;
Method that does load by id:
private NotaFiscal loadLazy(Long id) {
try {
String sql = "select o from NotaFiscal as o "
+ "left outer join o.listaEmail le "
+ "left outer join o.listaAutXml la "
+ "left outer join o.pagamentos p "
+ "left outer join o.duplicatas du "
+ "left outer join o.itens i "
+ "left outer join o.listaDocRef ldr "
+ "left outer join o.listaProcRef lpr "
+ "left outer join o.listaObsCont obs "
+ "left outer join o.eventos eve "
+ "left outer join o.volumes volumes "
+ "left outer join o.veiculos vei "
+ "where o.id = :id ";
Query query = getEntityManager().createQuery(sql);
query.setParameter("id", id);
return (NotaFiscal) query.getSingleResult();
} catch (Exception e) {
CriareLog.log(e);
return null;
}
}
Method where error occurs when requesting the list.
public DataModel<NotaFiscalEmail> getEmailsDM() {
if (emailsDM == null) {
if (this.getBean().getListaEmail() == null) {
this.getBean().setListaEmail(new ArrayList<NotaFiscalEmail>());
}
// O erro ocorre exatamente nesta linha.
emailsDM = new ListDataModel<NotaFiscalEmail>(this.getBean().getListaEmail());
}
return emailsDM;
}
If you need anything else go tell me that I'm posting here. I thank the help of all you. Hugs