【发布时间】:2016-06-22 20:16:36
【问题描述】:
我正在从其他来源获取产品和类别。我想将它们保存在阔叶数据库中。我不想要自动生成的 ID。我想将 id 来自源(从我获取产品的地方等)持久化到阔叶数据库。
为此,我创建了我的自定义控制器:http://www.broadleafcommerce.com/docs/core/current/broadleaf-concepts/admin/admin-custom-controllers
我正在 MyController 中编写产品和类别持久化代码。
我尝试了以下代码:
Category category = catalogService.createCategory();
Long categoryId = new Long(453510);
category.setId(categoryId);
category.setName("test category4");
category.setUrl("/test-category4");
catalogService.saveCategory(category);
Product p = catalogService.createProduct(ProductType.PRODUCT);
Sku newSku = catalogService.createSku();
Long skuId = new Long(453520);
newSku.setId(skuId);
p.setDefaultSku(newSku);
p.getDefaultSku().setDefaultProduct(p);
p.setName("test product4");
p.setUrl("/test-product4");
p.setCategory(category);
Long productId = new Long(453530);
p.setId(productId);
catalogService.saveProduct(p);
我正在关注堆栈跟踪:
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "blc_sku" violates foreign key constraint "fk28e82cf77e555d75"
Detail: Key (default_product_id)=(453530) is not present in table "blc_product".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:495)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:441)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
如果我在 sn-p 下删除,那么它会给我空指针,因为设置默认 sku 需要产品对象。
Sku newSku = catalogService.createSku();
Long skuId = new Long(453520);
newSku.setId(skuId);
p.setDefaultSku(newSku);
p.getDefaultSku().setDefaultProduct(p);
请帮助我将具有给定 ID(非自动生成)的产品保存在阔叶数据库中。
【问题讨论】:
标签: java spring hibernate broadleaf-commerce