带有实体Map的实体列表的JPA注释

时间:2017-03-07 17:56:11

标签: java excel jpa jsf-2

我得到了以下实体:

@Entity
public class Month extends BaseEntity {

private int year;
private String month;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Record> records;

}

BaseEntity有一个ID,所以这里没问题。

记录看起来像这样:

@Entity
public class Record extends BaseEntity {

@Temporal(TemporalType.DATE)
private Date date = new Date();

@ElementCollection
@CollectionTable(name = "record_item")
private Map<Category, Item> items;
}

ItemCategory只是带有标签(String)的普通实体。

所有实体都有构造函数,getter和setter!

我通过NamedQuery加载Month,如果我想显示数据表(普通jsf)中的记录,我得到以下异常:

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)

我的目标是使用JPA“模拟”Java中的Excel数据结构,如下所示:

Excel mapping

我正在获取如下所示的记录列表:

@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);
  }
}

这只给我一个record,但这些项目是PersistentBag

对于每个record我想显示keySet items,如下所示:

#{record.items.keySet()}

出了什么问题?您是否有任何提示如何使用正确的映射?

0 个答案:

没有答案
相关问题