插入新实体不会查看“SEQUENCE_GENERATOR” - 演示管理员

时间:2014-05-06 01:53:16

标签: broadleaf-commerce

我正在使用带有hibernate SQLServer2008Dialect方言的SQL Server 2012 Express Edition来运行Admin演示,并且在生成主键时遇到一些麻烦。初始插入语句不使用'SEQUENCE_GENERATOR'字段中@Id的预先计算的值。

@Id
@GeneratedValue(generator = "StructuredContentFieldId")
@GenericGenerator(
    name="StructuredContentFieldId",
    strategy="org.broadleafcommerce.common.persistence.IdOverrideTableGenerator",
    parameters = {
        @Parameter(name="segment_value", value="StructuredContentFieldImpl"),
        @Parameter(name="entity_name", value="org.broadleafcommerce.cms.structure.domain.StructuredContentFieldImpl")
    }
)
@Column(name = "SC_FLD_ID")
protected Long id;

尝试插入新的结构化内容时,'SEQUENCE_GENERATOR'表会填充一些值:

SELECT * FROM dbo.SEQUENCE_GENERATOR

ID_NAME                     ID_VAL
--------------------------- --------------------
SandBoxImpl                 101
StructuredContentFieldImpl  101
StructuredContentImpl       101

但新实体的id保存为1(根据demo sql脚本,此表中已存在一些现有行):

SELECT SC_ID, CONTENT_NAME, SC_TYPE_ID FROM dbo.BLC_SC

SC_ID                CONTENT_NAME                               SC_TYPE_ID
-------------------- ------------------------------------------ --------------------
1                    html test                                  2
100                  Buy One Get One - Twice the Burn           1
[...]
156                  Home Page Featured Products Title          3

插入该行时,控制台中会显示以下sql:

[artifact:mvn] Hibernate: select tbl.ID_VAL from SEQUENCE_GENERATOR tbl with (updlock, rowlock ) where tbl.ID_NAME=?
[artifact:mvn] Hibernate: update SEQUENCE_GENERATOR set ID_VAL=?  where ID_VAL=? and ID_NAME=?
[artifact:mvn] Hibernate: insert into BLC_SC (ARCHIVED_FLAG, CREATED_BY, DATE_CREATED, DATE_UPDATED, UPDATED_BY, CONTENT_NAME, DELETED_FLAG, LOCALE_CODE, LOCKED_FLAG, OFFLINE_FLAG, ORIG_ITEM_ID, ORIG_SANDBOX_ID, PRIORITY, SANDBOX_ID, SC_TYPE_ID, SC_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

稍后将一些HTML内容保存到BLC_SC_FLD并不是那么幸运。新实体也被赋予id 1,遗憾的是它已经存在:

SELECT SC_FLD_ID, FLD_KEY, VALUE, SC_ID FROM dbo.BLC_SC_FLD    

SC_FLD_ID     FLD_KEY       VALUE                                         SC_ID
------------- ------------- --------------------------------------------- --------
1             imageUrl      /img/banners/buy-one-get-one-home-banner.jpg  100

当然会抛出异常:

[artifact:mvn] Hibernate: update SEQUENCE_GENERATOR set ID_VAL=?  where ID_VAL=? and ID_NAME=?      
[artifact:mvn] Hibernate: insert into BLC_SC_FLD (CREATED_BY, DATE_CREATED, DATE_UPDATED, UPDATED_BY, FLD_KEY, LOB_VALUE, VALUE, SC_ID, SC_FLD_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
[artifact:mvn] 2014-05-06 00:58:02.191:WARN:oejs.ServletHandler:/admin/structured-content/1
[artifact:mvn] org.springframework.dao.DataIntegrityViolationException: Violation of PRIMARY KEY constraint 'PK__BLC_SC_F__8A534C1863E06FD9'. Cannot insert duplicate key in object 'dbo.BLC_SC_FLD'. The duplicate key value is (1).; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Violation of PRIMARY KEY constraint 'PK__BLC_SC_F__8A534C1863E06FD9'. Cannot insert duplicate key in object 'dbo.BLC_SC_FLD'. The duplicate key value is (1).

我不确定问题出在哪里。 @GenericGenerator生成策略org.broadleafcommerce.common.persistence.IdOverrideTableGenerator似乎在第一次插入时点击'SEQUENCE_GENERATOR',然后按照设计从FIELD_CACHE变量增加id。

所以我实际上有两个问题。

  1. 当表中已保存更高的ID时,为什么'SEQUENCE_GENERATOR'的初始值为101?
  2. 为什么要保存实体的值为1?这个MS SQL Server是否相关?

1 个答案:

答案 0 :(得分:1)

Ok解析:) Broadleaf有3个持久性单元,默认情况下它们指向同一个数据库,但只有一个(blPU)持久性单元在演示开始时导入sql。

通过这样做:

blPU.hibernate.hbm2ddl.auto=create-drop
blCMSStorage.hibernate.hbm2ddl.auto=create-drop
blSecurePU.hibernate.hbm2ddl.auto=create-drop

我让SEQUENCE_GENERATOR被删除并被其他持久性单元重新创建为空。

这很好用:

blPU.hibernate.hbm2ddl.auto=create-drop
blCMSStorage.hibernate.hbm2ddl.auto=update
blSecurePU.hibernate.hbm2ddl.auto=update

DOOH!