我是Broadleaf的新手,已經花時間與Broadleaf Demo項目熟悉框架。 我正在使用當前穩定的Broadleaf版本 - v2.2。Broadleaf - 創建動態產品異常
我的目標是在不使用管理控制檯的情況下創建動態類別/產品。但是,我得到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;
}
有人能解釋爲什麼我收到此異常(因爲類已經持續),我該如何解決它?