-->
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.  [ 9 posts ] 
Author Message
 Post subject: one-to-one mapping error when saveOrUpdate()
PostPosted: Wed Mar 18, 2009 11:43 am 
Newbie

Joined: Fri May 25, 2007 3:58 pm
Posts: 10
I have a parent/child one-to-one relation ship between Employee and Employment, using the same primary key between tables. When I add an Employee, using saveOrUpdate(), the insert gets called for the EMPLOYEE table, and then an update gets called for the EMPLOYMENT table which give me the following error:

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


What am I doing wrong?


Hibernate version:
3.2

Name and version of the database you are using:
MySQL

Mapping documents:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class schema="ABCMSTR" table="EMPLOYEE" name="com.abc.domain.Employee">
    <id unsaved-value="undefined" name="id" type="string" column="EMPLOYEE_ID" length="10">
      <generator class="assigned"/>
    </id>
    <one-to-one name="employment" class="com.abc.webtime.domain.Employment" cascade="all"/>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class schema="WEBTIME" table="EMPLOYMENT" name="com.abc.webtime.domain.Employment">
    <id name="employeeId" column="EMPLOYEE_ID">
      <generator class="foreign">
        <param name="property">employee</param>
      </generator>
    </id>
    <one-to-one name="employee" constrained="true" class="com.abc.domain.Employee"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 12:48 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
It is a 100% valid one-to-one relationship mapping but the problem

is I think that the employee obj it is already persistent (so it does an update and not insert) and in the employment doesn't exist but hibernate presume that it is(it does an update on employment) because of constrained=true.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 1:37 pm 
Newbie

Joined: Fri May 25, 2007 3:58 pm
Posts: 10
I removed the constrained=true and I still received the same error message with the same insert and update happening.

I've included part of the stack trace, in case it would be helpful.

Code:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:654)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:377)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:686)
   at com.abc.webtime.dao.hbm.HibernateEmployeeDaoImpl.saveEmployee(HibernateEmployeeDaoImpl.java:30)
   at com.abc.webtime.service.impl.EmployeeServiceImpl.saveEmployee(EmployeeServiceImpl.java:34)
   at com.abc.webtime.controller.EditUserController.onSubmit(EditUserController.java:84)
   at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
   at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
   at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:859)


Is there any other information which would be helpful?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 2:44 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
try to isolate the test case into something like this

...
Employee o = new Employee();
Employement o2 = new Employment();
o.setEmployment(o2);
session.save(o);
...


And send the actual output from sysout when the properties

hibernate.show_sql=false
hibernate.format_sql=true


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 3:09 pm 
Newbie

Joined: Fri May 25, 2007 3:58 pm
Posts: 10
Here is the output from my console. As far as isolating the save with new Employee and new Employment, I'm not able to do this easily as there are many required fields and in the end it will end up looking the same as what I have below. FYI - I also have the parameters being written to the log to give additional detail.

Thank you for your help.

Code:
Hibernate: select employee_.EMPLOYEE_ID, employee_.SUPERVISOR_ID as SUPERVISOR2_1_, employee_.FIRST_NAME as FIRST3_1_, e
mployee_.MIDDLE_NAME as MIDDLE4_1_, employee_.LAST_NAME as LAST5_1_, employee_.EMAIL as EMAIL1_, employee_.ADDED_BY as A
DDED7_1_, employee_.ADDED_AT as ADDED8_1_, employee_.CHANGED_BY as CHANGED9_1_, employee_.CHANGED_AT as CHANGED10_1_ fro
m ABCMSTR.EMPLOYEE employee_ where employee_.EMPLOYEE_ID=?
2009-03-18 13:56:59,270 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'SAD0001' to parameter: 1

Hibernate: insert into ABCMSTR.EMPLOYEE (SUPERVISOR_ID, FIRST_NAME, MIDDLE_NAME, LAST_NAME, EMAIL, ADDED_BY, ADDED_AT, C
HANGED_BY, CHANGED_AT, EMPLOYEE_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2009-03-18 13:56:59,280 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding null to parameter: 1

2009-03-18 13:56:59,290 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'Sherry' to parameter: 2

2009-03-18 13:56:59,300 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'Ann' to parameter: 3

2009-03-18 13:56:59,300 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'Smith' to parameter: 4

2009-03-18 13:56:59,310 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'craig.smith@public.com' to parameter: 5

2009-03-18 13:56:59,320 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'craigssmith' to parameter: 6

2009-03-18 13:56:59,330 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding '18 March 2009' to parameter: 7

2009-03-18 13:56:59,330 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'craigssmith' to parameter: 8

2009-03-18 13:56:59,340 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding '18 March 2009' to parameter: 9

2009-03-18 13:56:59,350 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'SAD0001' to parameter: 10

Hibernate: update WEBTIME.EMPLOYMENT set PAYROLL_ID=?, EXEMPT=?, HIRE_DATE=?, TERMINATE_DATE=?, ADDED_BY=?, ADDED_AT=?,
CHANGED_BY=?, CHANGED_AT=? where EMPLOYEE_ID=?
2009-03-18 13:56:59,360 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'SAD0001' to parameter: 1

2009-03-18 13:56:59,370 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'false' to parameter: 2

2009-03-18 13:56:59,380 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding '01 March 2009' to parameter: 3

2009-03-18 13:56:59,390 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding null to parameter: 4

2009-03-18 13:56:59,390 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'craigssmith' to parameter: 5

2009-03-18 13:56:59,400 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding '18 March 2009' to parameter: 6

2009-03-18 13:56:59,410 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'craigssmith' to parameter: 7

2009-03-18 13:56:59,420 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding '18 March 2009' to parameter: 8

2009-03-18 13:56:59,420 TRACE: [org.apache.commons.logging.impl.Log4JLogger]
    binding 'SAD0001' to parameter: 9

2009-03-18 13:56:59,430 ERROR: [org.apache.commons.logging.impl.Log4JLogger]
    Exception executing batch:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expe
cted: 1
        at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
        at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
        at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 7:04 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
I made myself a test to see if something is wrong and I get the expected result so the problem is in another area. I will post the code:


Test Case

...

public void test(){
Employee o = new Employee();
Employment e = new Employment();

o.setEmployment(e);
e.setEmployee(o);

o.setId("AS1002");

getSession().saveOrUpdate(o);
getSession().flush();
}


mapping:

identic with your mappings


classes:

public class Employee {
private String id;
private Employment employment;

public Employment getEmployment() {
return employment;
}

public void setEmployment(Employment employment) {
this.employment = employment;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}


public class Employment {
private String employeeId;
private Employee employee;

public Employee getEmployee() {
return employee;
}

public void setEmployee(Employee employee) {
this.employee = employee;
}

public String getEmployeeId() {
return employeeId;
}

public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
}


sysoutput:

Hibernate:
select
employee_.EMPLOYEE_ID
from
EPOS.EMPLOYEE employee_
where
employee_.EMPLOYEE_ID=?
Hibernate:
insert
into
EPOS.EMPLOYEE
(EMPLOYEE_ID)
values
(?)
Hibernate:
insert
into
EPOS.EMPLOYMENT
(EMPLOYEE_ID)
values
(?)


and the result in the tables(EMPLOYEE and EMPLOYMENT) are the expected one.

Maybe you have something wrong in the java classes Employee or Employment.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 8:47 am 
Newbie

Joined: Fri May 25, 2007 3:58 pm
Posts: 10
Thank you for your help. It solved my problem.

I was setting the id of the Employment class. As soon as I removed that and changed the order of setting the Employment on the Employee and the Employee on the Employment, both inserts happened.

Is there good documentation somewhere on issues of saving data to the database (like in this case), as far as specifics on how to set up the objects.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:23 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
The best book is

'Java Persistence with Hibernate' from Manning.

you can find that on amazon.


Good luck!


Top
 Profile  
 
 Post subject: Re: one-to-one mapping error when saveOrUpdate()
PostPosted: Wed Apr 28, 2010 7:40 am 
Newbie

Joined: Wed Jul 26, 2006 4:18 pm
Posts: 8
Location: Brussels, Belgium
Very much the same problem ...
Very much the same issue ...

Insert happens when I saveOrUpdate ...
The next statement is an update (while I would expect an "insert") ... and it is send to the DB when I commit the Transaction.

Code:
Hibernate:
    insert
    into
        XXX.USER
        (logonuid, displayname, firstname, lastname, department, islocked, jobfunction, ...)
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ...)
Hibernate:
    update
        XXX.USER_SEARCH
    set
        logonuid=?,
        e_mail=?,
        firstname=?,
        lastname=?,
        search_field=?
    where
        USER_ID=?
13:27:10,183 ERROR AbstractBatcher:51 - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)


For me (I am still investigating it), the only difference is that my PK in User is defined as follows (prior to filling the key's with the expected values) :
Code:
<hibernate-mapping package="com.myCompany.xxx.yyy.dao">
  <class name="UserTo" schema="@SCHEMA_NAME@" table="@USER_TABLE@" mutable="true" batch-size="1000">
    <id name="id" column="USER_ID">
      <generator class="identity" />
    </id>
....

    <!--  -->
    <one-to-one name="searchFields" class="UserSearchTo" cascade="all" />
  </class>
</hibernate-mapping>

Code:
<hibernate-mapping package="com.myCompany.xxx.yyy.dao">
  <class name="UserSearchTo" schema="@SCHEMA_NAME@" table="USER_SEARCH" mutable="true" batch-size="1000">
    <id name="id" column="USER_ID" >
      <generator class="foreign">
        <param name="property">user</param>
      </generator>
    </id>
    <!-- longonuid + config_id should also make a PK -->
    <property name="logonuid"          column="logonuid"      not-null="true"/>
    <property name="email"             column="e_mail"        not-null="true" />
    <property name="firstName"         column="firstname"     not-null="true"/>
    <property name="lastName"          column="lastname"      not-null="true" />
    <property name="searchField"       column="search_field"  not-null="true"  length="500" />

    <one-to-one name="user" class="UserTo" constrained="true" />

  </class>
</hibernate-mapping>


I doubt the "identity" is the issue...

Any help is appreciated,

\T,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.