Hi,
I'm using Spring together with hibernate, I configure my hibernate setting with annotations. Now I have the following (simplified) table structure
report 1 : n reportColumn
In the report class I have a List with Column items, which are bound through a onetomany annotations:
Code:
@OneToMany(mappedBy="report", fetch=FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@OrderBy(clause = "column_order_nr asc")
private List<ReportColumn> columns = new ArrayList<ReportColumn>();
In the column class, I have a reference to the report class, configured with a manytoone annotation:
Code:
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="report_column_report_id")
private Report report;
When I try to load all reports in my dao, there are too much report items returned:
Code:
public ArrayList<Report> getReports() {
ArrayList<Report> reports =
(ArrayList<Report>)this.getHibernateTemplate().loadAll(Report.class);
return reports;
}
The loadAll-method returns a report for each column. E.g. when I have a report with two columns, the loadAll-method returns the same report twice.
I already tried to avoid this by removing the FetchType.EAGER setting. Than the loadAll-method returns the expected number of reports. But when I save a selected report in a session bean and than try to access the columns of the report in another request, the following error occures:
Quote:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: de.dyspepsie.fode.doc.Report.columns, no session or session was closed
I already use the org.springframework.orm.hibernate3.support.OpenSessionInViewFilter provided by spring, but this keeps the session opened only for one request...
Do you have an idea how to handle this?
Thanks