I got following entity:
@Entity
public class Month extends BaseEntity {
private int year;
private String month;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Record> records;
}
BaseEntity
has an ID, so that is no problem here.
Record looks like this:
@Entity
public class Record extends BaseEntity {
@Temporal(TemporalType.DATE)
private Date date = new Date();
@ElementCollection
@CollectionTable(name = "record_item")
private Map<Category, Item> items;
}
Item
and Category
are just plain entities with a label(String).
All entities got constructors, getters and setters!
I am loading via NamedQuery the Month
and if I want to disply the records in a datatable (plain jsf), I got following exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: de.financeme.model.MonthOverview.records, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:567)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:205)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:146)
at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
at javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:109)
at javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:184)
at javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:219)
at javax.faces.model.ListDataModel.<init>(ListDataModel.java:78)
at javax.faces.component.UIData.getDataModel(UIData.java:1828)
at javax.faces.component.UIData.getRowCount(UIData.java:356)
My goal ist to 'simulate' an excel data structure in Java with JPA like this:
I am getting the list of records like shown below:
@Stateless
public class MonthOverviewService extends AbstractEntityService<MonthOverview> {
public MonthOverview findByMonthYear(String month, int year) {
TypedQuery<MonthOverview> query = getEm().createNamedQuery(MonthOverview.NQ_FIND_BY_MONTH_YEAR, MonthOverview.class);
query.setParameter("month", month);
query.setParameter("year", year);
return query.getResultList().get(0);
}
}
This gives me exactly one record
, but the items are a PersistentBag
.
For each record
I want to display the keySet
of items
like this:
#{record.items.keySet()}
What is going wrong? Do you have any hints how to use a correct mapping?