I am new to Broadleaf and have been spending time with Broadleaf Demo project to familiarize with the framework. I am using the current stable Broadleaf version - v2.2.

My aim is to create a dynamic category/product without using the Admin console. However, I am getting 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)

This is how I am trying to create category/product:

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

Can someone explain why I am getting this exception( since category has been persisted) and how do I resolve it?

有帮助吗?

解决方案 2

I figured out the fix. Modified the line : catalogService.saveCategory(category); to category = catalogService.saveCategory(category); and I am no longer getting the exception.

其他提示

Looks to me like catagories list is empty, resulting in a call to service that fetches you new Category. Now, though you are associated this new category with product, but this new category is not saved in database.

You need to persist this new category first and than call save on product. This should resolve the issue.

And if this category is persisted than somehow hibernate is not able match this category with the one that you think is persisted.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top