【问题标题】:LazyInitializationException when collection is loaded in a BeanItemContainer在 BeanItemContainer 中加载集合时出现 LazyInitializationException
【发布时间】:2012-08-01 18:32:51
【问题描述】:

我正在为我的 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 实现

【问题讨论】:

    标签: hibernate vaadin


    【解决方案1】:

    这是由于 Hibernate 在访问时延迟获取关系(例如,在您的特定情况下访问“favoriteUsersTo”)并且没有可用于获取的 Hibernate Session。

    我建议您考虑改用JPAContainer。它将为您处理休眠会话。

    【讨论】:

    • JPAContainer 显然不是免费的。关于免费解决方案的建议?
    • 我找到了解决办法,实现你自己的容器,只在public static final Object[] NATURAL_COL_ORDER = new Object[] { "field1", "field2" };中选择你想要的属性
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多