-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ERROR - Could not synchronize database state with session
PostPosted: Wed Apr 28, 2004 12:09 pm 
Newbie

Joined: Wed Apr 28, 2004 12:02 pm
Posts: 1
Hello,

I am getting the following error stack trace:

Hibernate: insert into order1 (userid, orderdate, orderamount, orderid) values (?, ?, ?, ?)
Hibernate: update orderitem set itemid=?, itemname=?, itemprice=?, itemqty=?, order=? where orderitemid=?
ERROR - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.persistence.hibernate.OrderTest.storeOrder(OrderTest.java:48)
at com.persistence.hibernate.OrderTest.main(OrderTest.java:55)

[u]Mapping Files:[/u]

[b]Order:[/b]

<class name="com.persistence.hibernate.Order" table="order1">
<id name="orderid" column="orderid" type="int">
<generator class="increment"></generator>
</id>
<property name="userid" type="java.lang.String" column="userid"/>
<property name="orderdate" type="timestamp" column="orderdate"/>
<property name="orderamount" type="double" column="orderamount"/>

<set name="orderItems" table="orderitem" inverse="true" cascade="all">
<key column="order_orderid"/>
<one-to-many class="com.persistence.hibernate.OrderItem"/>
</set>
</class>

[b]OrderItem[/b]

<class name="com.persistence.hibernate.OrderItem" table="orderitem">
<id name="orderitemid" column="orderitemid" type="int">
<generator class="increment"></generator>
</id>
<property name="itemid" type="int" column="itemid"/>
<property name="itemname" type="java.lang.String" column="itemname"/>
<property name="itemprice" type="double" column="itemprice"/>
<property name="itemqty" type="int" column="itemqty"/>
<property name="orderId" type="int" column="order_orderid" insert="false" update="false" />
<many-to-one name="order" class="com.persistence.hibernate.Order" />

</class>

[b]Java Code Snippets[/b]

public void addProduct() {
OrderItem item = new OrderItem();
item.setItemname("j2ee test");
item.setItemprice(1111.11);
item.setItemid(77);
item.setItemqty(5);
//set the order for the order item
item.setOrder(this);
//this.priceTotal = this.priceTotal + p.getPrice() * amount;
this.orderItems.add(item);
}

[b]Test Class Method[/b]

private void storeOrder(String userId, double amt) {
try {

Order order = new Order();
order.setUserid(userId);
order.setOrderdate(new Timestamp(new Date().getTime()));
order.setOrderamount(amt);


order.addProduct();

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(order);
tx.commit();
} catch (HibernateException hce) {
hce.printStackTrace();
}
}

Please help.

Thanks.








[b][/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 28, 2004 12:45 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
You need to set your unsaved-value for the id mapping on both your Order and OrderItem. Currently, when you try to save your Order with OrderItem, it thinks that your OrderItem already exists in the DB and so attempts and update instead.

For example:

Order mapping file:

Code:
<class name="com.persistence.hibernate.Order" table="order1">
    <id name="orderid" column="orderid" type="int" unsaved-value="0">
        <generator class="increment"></generator>
    </id>

    ....


OrderItem mapping file:

Code:
<class name="com.persistence.hibernate.OrderItem" table="orderitem">
    <id name="orderitemid" column="orderitemid" type="int" unsaved-value="0">
        <generator class="increment"></generator>
    </id>

    ....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.