2012-08-01 50 views
0

我正在为我的jpa实体构建一个CRUD视图。一旦我用jpa容器检索到实体的实例,每次选择一个实例时,我都必须加载延迟初始化的集合(我将它加载到BeanItemContainer中)。所以,我做了以下内容:LazyInitializationException在BeanItemContainer中加载集合时

   BeanItemContainer<ModelItem> beans = 
        new BeanItemContainer<ModelItem>(ModelItem.class); 

      Property property = item.getItemProperty(propertyID); 


      if (property.getType().equals(Collection.class)){ 
       beans.addAll((Collection<? extends ModelItem>) property.getValue()); 
      } 

      Class<? super ModelItem> clazz = beans.getBeanType(); 

      PropertyDescriptor properties[]=PropertyUtils.getPropertyDescriptors(clazz); 

      ArrayList<Object> tablePropertiesList=new ArrayList<Object>(); 

      for (PropertyDescriptor propertyDescriptor : properties) { 
       if (!(propertyDescriptor.getPropertyType().equals(Collection.class)) && !(propertyDescriptor.getName().equals("class"))){ 
        tablePropertiesList.add(propertyDescriptor.getName()); 
       } 
      } 

      Object tableProperties[]=transform(tablePropertiesList); 

      Table currentTable = collections.get(propertyID); 
      currentTable.setContainerDataSource(beans); 
      currentTable.setVisibleColumns(tableProperties); 

在这种情况下,我得到LazyInitializationException中

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.windy.server.model.core.UserModel.favoriteUsersTo, no session or session was closed 

我该如何解决这个问题,vaadin框架?我使用Hibernate作为JPA实现

PS

回答

0

这是由于休眠时的访问延迟抓取关系(例如访问你的具体情况“favoriteUsersTo”)并没有Hibernate的Session可用做抓取。

我建议你看看使用JPAContainer来代替。它会为你处理休眠会话。

+0

JPAContainer显然不可用。有免费解决方案的建议? – 2012-09-04 11:59:41

+0

我找到了一个解决方案,实现你自己的容器,只在'public static final Object [] NATURAL_COL_ORDER = new Object [] { \t \t“field1”,“field2”}; – 2012-09-04 12:21:23