【问题标题】:Broadleaf - Create Dynamic Product ExceptionBroadleaf - 创建动态产品例外
【发布时间】:2013-06-26 02:22:29
【问题描述】:

我是 Broadleaf 的新手,一直在花时间研究 Broadleaf Demo 项目以熟悉该框架。 我正在使用当前稳定的 Broadleaf 版本 - v2.2。

我的目标是在不使用管理控制台的情况下创建动态类别/产品。但是,我得到 TransientObjectException

java.lang.IllegalStateException: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: org.broadleafcommerce.core.catalog.domain.CategoryImpl
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1386)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)

这就是我尝试创建类别/产品的方式:

private void addProductMetaData(Product product) {
        product.setName("phone A");
        product.setFeaturedProduct(true);
        product.setUrl("/phones/phoneA");
        product.setCanSellWithoutOptions(true);
        product.setManufacturer("manufacturer A");
        product.setActiveStartDate(new Date());
        product.setActiveEndDate(null);
        addMediaInformation(product);

        Category category = retrieveCategory();
        product.setDefaultCategory(category);
        // product.getAllParentCategories().add(category);
        catalogService.saveProduct(product); //**Exception occurs here**

    }

 private Category retrieveCategory() {
        List<Category> categories = catalogService.findCategoriesByName("phones");
        Category category = (CollectionUtils.isEmpty(categories) ? catalogService.createCategory() : categories.get(0));
        if (StringUtils.isEmpty(category.getName())) {
            category.setName("phones");
            category.setUrl("/phones");
            Category parentCategory = catalogService.findCategoriesByName("Primary Nav").get(0);
            category.setDefaultParentCategory(parentCategory);
            category.setActiveStartDate(new Date());
            category.getAllParentCategories().add(parentCategory);
            catalogService.saveCategory(category);
        }
        return category;
    }

有人可以解释为什么我会收到此异常(因为类别已被保留)以及如何解决它?

【问题讨论】:

    标签: java spring hibernate broadleaf-commerce


    【解决方案1】:

    在我看来,类别列表为空,导致调用服务来获取新类别。现在,虽然您将此新类别与产品相关联,但此新类别并未保存在数据库中。

    您需要先保留这个新类别,然后再调用 save on product。这应该可以解决问题。

    如果这个类别是持久的,那么 hibernate 无法将这个类别与你认为持久的类别相匹配。

    【讨论】:

      【解决方案2】:

      我想出了解决办法。修改线路:catalogService.saveCategory(category);category = catalogService.saveCategory(category);,我不再收到异常。

      【讨论】:

        猜你喜欢
        • 2022-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        相关资源
        最近更新 更多