当我尝试更新对象 RFQ 时,Hibernate 倾向于更新其子集合 BQMaster 并尝试设置和更新 BQMaster 的标识符,这通常是不允许的。
postgre 日志文件:
2009-05-22 11:16:53 错误:重复键违反唯一约束“bq_masters_pkey”
2009-05-22 11:16:53 声明:更新 bq_masters 设置 rfq_id=$1,id=$2 where id= 3 美元
控制台错误:
原因:java.sql.BatchUpdateException:批处理条目 0更新 bq_masters set rf q_id=2, id=0 where id=49449 was aborted。调用 getNextException 查看原因。在org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutor Impl.java:1257)在 org.postgresql.core.v3.QueryExecutorImpl
的 org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handle Error(AbstractJdbc2Statement.java:2392) org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:58)的org.hibernate.execute( QueryExecutorImpl.ja va:334) .jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 32 更多
当它试图更新 bq_masters 的 id 时,这似乎很奇怪。它应该保留 id 并仅更新其他字段。
Criteria crit = session.createCriteria(BQMaster.class);
crit.add(Restrictions.eq("project",project);
crit.setFirstResult(5);
crit.setMaxResult(2);
List bqMasters = crit.list();
RFQ rfq = (RFQ)session.get(RFQ.class, RFQId);
rfq.setBqMasters(bqMasters);
session.update(rfq);
休眠映射:
<class name="RFQ" table="rfq">
<id name="id" column="id">
<generator class="native">
<param name="sequence">rfq_id_seq</param>
</generator>
</id>
<property name="reference" column="reference" />
<property name="status" column="status" />
<property name="created" column="created" />
<property name="modified" column="modified" />
<many-to-one name="project" class="Project" column="project_id" cascade="save-update" />
<list name="subcons" table="rfq_subcons">
<key column="rfq_id" />
<list-index column="id"/>
<many-to-many class="Subcon" column="subcon_id"/>
</list>
<list name="bqMasters">
<key column="rfq_id" />
<list-index column="id"/>
<one-to-many class="BQMaster"/>
</list>
</class>
<class name="BQMaster" table="bq_masters">
<id name="id" column="id">
<generator class="native">
<param name="sequence">bq_masters_id_seq</param>
</generator>
</id>
<property name="reference" column="reference" />
<property name="description" column="description" />
<property name="lod" column="lod" />
<property name="uom" column="uom" />
<property name="quantity" column="quantity" />
<property name="parentId" column="parent_id" />
<property name="groupId" column="group_id" />
<property name="leaf" column="leaf" />
<property name="active" column="active" />
<property name="subcontract" column="subcontract" />
<property name="created" column="created" />
<property name="modified" column="modified" />
<many-to-one name="project" class="Project" column="project_id" cascade="save-update" />
<many-to-one name="page" class="Page" column="page_id" cascade="save-update" />
<many-to-one name="rfq" class="RFQ" column="rfq_id" cascade="save-update" />
</class>
数据库结构供参考:
CREATE TABLE rfq
(
id bigserial NOT NULL,
project_id bigint,
reference text,
status text,
created timestamp without time zone,
modified timestamp without time zone,
CONSTRAINT rfq_pkey PRIMARY KEY (id),
CONSTRAINT rfq_project_id_fkey FOREIGN KEY (project_id)
REFERENCES projects (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE bq_masters
(
id bigserial NOT NULL,
reference varchar(100),
project_id bigint,
page_id bigint,
rfq_id bigint,
description text,
lod integer,
uom varchar(20),
quantity numeric,
parent_id bigint,
group_id bigint,
leaf boolean,
active boolean,
subcontract boolean,
created timestamp without time zone,
modified timestamp without time zone,
CONSTRAINT bq_masters_pkey PRIMARY KEY (id),
CONSTRAINT bq_masters_project_id_fkey FOREIGN KEY (project_id)
REFERENCES projects (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT bq_masters_page_id_fkey FOREIGN KEY (page_id)
REFERENCES pages (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT bq_masters_rfq_id_fkey FOREIGN KEY (rfq_id)
REFERENCES rfq (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE
);
我尝试在 RFQ 一对多映射中添加 inverse="true" 但无济于事,因为它不会更新相应 RFQ 对象的 BQMasters。
有人对解决这个问题有任何线索吗?提前致谢。