我正在为我的 jpa 实体构建一个 CRUD 视图。一旦我每次选择一个实例时都使用 jpa 容器检索我的实体实例,我必须加载延迟初始化的集合(我将它加载到 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 框架解决这个问题?
PS 我使用 Hibernate 作为 JPA 实现