I have 2 tables(parent/child)
TEST1(
ID NUMBER(19) not null,
NAME VARCHAR2(255),
primary key(ID)
)
TEST2(
OBJID NUMBER(19) not null,
VERSION NUMBER(10) not null,
NAME VARCHAR2(255),
primay key (OBJID, VERSION)
)
Alter table TEST2 add constraints fk foreign key (OBJID) references ID.
mappping file:
<class name="Test1" table="TEST1" >
<id name="id" type="long" column="ID">
<generator class="native" >
</generator>
</id>
<property name="name" column="NAME" type="string" />
<set name="test2s" table="TEST2" lazy="true" >
<key column="OBJID" />
<composite-element class="Test2">
<property name="version" />
<property name="fieldName" />
</composite-element>
</set>
</class>
codes between session:
Code:
Test1 test = new Test1();
Test2 test2 = new Test2();
test2.setVersion(new Integer(1));
HashSet set = new HashSet();
set.add(test2);
test.setTest2s(set);
persistenceSession.save(test);
After I run the codes,
I found that the sql query is working like this:
Hibernate: insert into TEST1 (NAME, ID) values (?, ?)
Hibernate: insert into TEST2 (OBJID, version, fieldName) values (?, ?, ?)
Hibernate: delete from TEST2 where OBJID=? and version=? and fieldName=?
Hibernate: insert into TEST2 (OBJID, version, fieldName) values (?, ?, ?)
Here the insert to table TEST2 2 times, first insert to TEST2, then delete from TEST2, then insert again! Very funny, I have only one record need to insert into TEST2. I guess after this deleting, i got the error like:
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (SCS.SYS_C001786) violated
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:458)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4105)
at weblogic.jdbc.wrapper.PreparedStatement_oracle_jdbc_driver_OraclePreparedStatement.executeBatch(Unknown Source)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:118)
I do not know which attribute incur this deletion action and then insert again? could anyone help?