0

This is my code to insert into two tables. But it gives me an error "Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1"

    Session session = sessionFactory.getCurrentSession();
    ProfileDTO profile = new ProfileDTO();
    profile.setCustomerID(1);
    profile.setProfileName(profileName);
    profile.setProfileType(profileType);
    profile.setRecordId(9);
    session.save(profile);

    int profileID = profile.getRecordId();
    CustomerMeasurementsDTO measurement = new CustomerMeasurementsDTO();
    String ids[] = profileidsString.split(",");
    String vals[] = profilevalsString.split(",");
    for (int i = 0; i < ids.length ; i++){
        measurement.setMeasurementId(ids[i]);
        measurement.setMeasurementValue(vals[i]);
        measurement.setCustMeasurementsProfileId(profileID);
        session.save(measurement);
    };

When executed, It gives this for logging purpose.

Hibernate: 
    /* insert com.domain.CustomerMeasurementsDTO
        */ insert 
        into
            cust_measurements
            (cust_measurements_record_id, last_modified, measurement_id, measurement_value, measurement_record_id) 
        values
            (?, ?, ?, ?, ?)
Hibernate: 
    /* update
        com.domain.CustomerMeasurementsDTO */ update
            cust_measurements 
        set
            cust_measurements_record_id=?,
            last_modified=?,
            measurement_id=?,
            measurement_value=? 
        where
            measurement_record_id=?

I want it to make an insert query again, while its trying to update. Please help me where I'm wrong.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41

3 Answers3

1

Put your CustomerMeasurementsDTO measurement = new CustomerMeasurementsDTO(); inside your loop. That should create a new record every loop iteration, instead of update it.

tito
  • 12,990
  • 1
  • 55
  • 75
  • I tried that but now it gives me "a different object with the same identifier value was already associated with the session" I really dont understand what either of the errors mean. Please can you explain me. – Dunes Buggy Mar 08 '12 at 21:38
0

Each object that you create must have a different id. use session.flash() you can also display paramaters of genereated SQL by using How to print a query string with parameter values when using Hibernate in that way you can see which id is being passed

Community
  • 1
  • 1
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30
0

I finally found a way out. In my hibernate.cfg.xml I set the property value of "hbm2ddl.auto" to 'create', which was 'update' earlier.

Although I dont know much about the security issues of this, but it works for me now fine.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41