- This topic has 3 replies, 2 voices, and was last updated 19 years, 1 month ago by
Haris Peco.
-
AuthorPosts
-
gummadi_bMemberThis is pretty basic stuff. All I am trying to do is insert a Customer.
I am using mysql, tomcat, spring, struts, hibernate.
Using myeclipse I reverseengineered all my tables which created hibernate mapping files, Hibernate Object and DAOs.When I use findById it works fine, but when I try to insert a customer it fails. Error I get is HibernateOptimisticLockingFailureException (Batch update returned unexpected row count from update: 0 actual row count: 0 exprected: 1″
Customer.hbm.xml
————————
has one id
<id name=”customerId” type=”integer”>
<column name=”CustomerId” />
<generator class=”identity” />
</id>
…
properties
…
<set name=”tktsOrders” inverse=”true”>
<key>
<column name=”CustomerId” not-null=”true” />
</key>
<one-to-many class=”com.dal.hibernate.dbo.Orders” />
</set>I inserted all values into customer object expect for orders. Hope this information helps you guys to help me.
Please advice me. What am I doing wrong?Thanks
Tech Lover
Haris PecoMemberTech,
Please, send us table ddl (create table) and code for saveOrUpdate. if it possible
What is your version eclipse and myeclipse and do you use hibernate libraries from MyEclipse ?Best regards
gummadi_bMemberYup, I am using hibernate libraries from MyEclipse. Eclipse version is 3.1 and the myeclipse version is 4.1.1.
After troubleshooting for couple of hours I found out that, BeanUtils is the culprit. I used BeanUtils.copyProperties, because of which my Id field is assigned a default value of 0(zero).I guess hibernate creates an update statement as opposed to insert statement because id field has a value.
I had to modify the hbm file and declare unsaved-value=”0″. Now it works fine.Is this the right way of doing it or if not can you please point me to the best practice of achieving this?
Thanks
Haris PecoMemberTech,
If it work then it is correct.
Best
-
AuthorPosts