0

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:

Excel mapping

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?

alexander
  • 1,191
  • 2
  • 20
  • 40
  • The exception seems to indicate a problem with how you're trying to access or use the entities, not with how you're mapping them. You haven't provided the information we would need to address that. – John Bollinger Mar 07 '17 at 17:58
  • @JohnBollinger I am sorry! I just added the service to clarify it. Do you need the controller aswell? – alexander Mar 07 '17 at 18:07
  • If Category and Item are entities, then you MUST use `@OneToMany` for the Map field – Neil Stockton Mar 08 '17 at 13:30

0 Answers0