Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I have two associated objects that have a bidirectional one-to-many association,I have exported their data to external xml sucessfully.Then I plan to import the xml data to the database and overwrite the object if it exists, or create new object if the row doesn't exist.When I overwrite, it does work, but when it should insert new row,exception happenes.
Thank you in advance!
Hibernate version:hibernate 3.0.4
Mapping documents:
The following is the hbm of the two table.:
<hibernate-mapping
package="cn.ac.iscas.itechs.qms.projectie.hibernate"
>
<class
name="ProjectInfo"
node="ProjectInfo"
table="project_info"
>
<id
column="PROJECT_ID"
name="Id"
node="@Id"
type="integer"
>
<generator class="increment" />
</id>
<!-- ReviewProduct one-to-many -->
<set
cascade="all,delete-orphan"
embed-xml="true"
inverse="true"
name="reviewProducts"
node="reviewProducts"
>
<key column="REVIEW_PROJECT_ID" on-delete="noaction" />
<one-to-many
class="cn.ac.iscas.itechs.qms.projectie.hibernate.ReviewProduct"
embed-xml="true"
node="ReviewProduct"
not-found="ignore"
/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping
package="cn.ac.iscas.itechs.qms.projectie.hibernate"
>
<class
name="ReviewProduct"
node="ReviewProduct"
table="review_product"
>
<id
column="REVIEW_PRODUCT_ID"
name="Id"
node="@Id"
type="integer"
>
<generator class="increment" />
</id>
<many-to-one
cascade="save-update"
class="cn.ac.iscas.itechs.qms.projectie.hibernate.ProjectInfo"
column="REVIEW_PROJECT_ID"
embed-xml="false"
insert="true"
lazy="false"
name="project"
node="ProjectInfo/@Id"
not-found="ignore"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
The code as follow:
Session dom4jSession = session.getSession(EntityMode.DOM4J);
transaction=session.beginTransaction();
SAXReader saxReader = new SAXReader();
document = saxReader.read(new FileInputStream(file));
result=document.selectNodes("//ProjectInfo");
Iterator proIterator=result.iterator();
while (proIterator.hasNext()) {
Object project = proIterator.next();
Element proElement=(Element)project; dom4jSession.replicate("cn.ac.iscas.itechs.qms.projectie.hibernate.ProjectInfo",project,ReplicationMode.OVERWRITE);
// dom4jSession.saveOrUpdate("cn.ac.iscas.itechs.qms.projectie.hibernate.ProjectInfo",project);
}
transaction.commit();
dom4jSession.close();
session.close();
The file which is read by saxreader is shown as follow:
<?xml version="1.0" encoding="UTF-8"?>
<ProjectInfo Id="25">
<reviewProducts>
<ReviewProduct Id="30">
<ProjectInfo Id="25"/>
</ReviewProduct>
</reviewProducts>
</ProjectInfo>
There I have deleted the row with id 25 in the projectinfo table and the row with id 30 in the reviewproduct table,so I hope the new object should be created and the new row should be inserted.
[b]Full stack trace of any exception that occurs:
When I use dom4jSession.replicate()
the exception as follow:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
When I use dom4jSession.saveOrUpdate()
the exception as follow:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:92)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:78)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:69)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:150)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1954)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
Name and version of the database you are using:
mysql4.0
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: