0

これは、2 つのテーブルに挿入するコードです。しかし、「バッチ更新が更新から予期しない行数を返しました[0];実際の行数:0;予想: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);
    };

実行すると、ログの目的でこれが提供されます。

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=?

更新しようとしている間に、もう一度挿入クエリを作成したい。私が間違っているところを助けてください。

4

3 に答える 3

1

CustomerMeasurementsDTO measurement = new CustomerMeasurementsDTO();ループの中に入れてください。更新するのではなく、ループの繰り返しごとに新しいレコードを作成する必要があります。

于 2012-03-08T20:32:26.950 に答える
0

作成する各オブジェクトには、異なるIDが必要です。session.flash()を使用して、生成されたSQLのパラメーターを表示することもできます。Hibernateを使用するときにパラメーター値を含むクエリ文字列を出力する方法を使用すると、どのIDが渡されているかを確認できます。

于 2012-03-08T23:12:03.880 に答える
0

私はついに道を見つけました。hibernate.cfg.xml で、「hbm2ddl.auto」のプロパティ値を「create」に設定しました。これは以前は「update」でした。

これのセキュリティの問題についてはあまり知りませんが、今ではうまく機能しています。

于 2012-03-09T06:46:09.240 に答える