0

In my project I use hibernate hbm and spring,i run an sql query to update a single column,

Query sql = getSession().createSQLQuery("update HISTORIQUE_DETAIL_APPELS  set token_otp =  '"+historiqueDetailAppelsVO.getCodeOtp()+"' where id =   '"+historiqueDetailAppelsVO.getId()+"'");
       try {
                   sql.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }

I found that another query is executed and update the table in data base,

Hibernate: update HISTORIQUE_DETAIL_APPELS  set token_otp = '14d3fc' where id = '150017'


Hibernate: update HISTORIQUE_DETAIL_APPELS set cod_cent=?, adresse_ip=?, id_conseiller=?, type_piece=?, num_piece_ident=?, msisdn=?, mois1_detail=?, mois2_detail=?, mois3_detail=?, date_demande=?, no_ticket_caisse=?, date_ticket=?, cod_user=?, dat_maj=?, flag_imp_data=?, date_imp_data=?, token_otp=?, send_mail=?, client_mail=?, date_debut=?, date_fin=? where id=?

where does the origin of the second update ?

CHHIBI AMOR
  • 1,186
  • 2
  • 13
  • 27

2 Answers2

1

I faced the same issue before, and after some research i found :

When user update any record by using direct query based operation, it is directly updated to database. But, if the same record(previous copy) is already present in current session(that is previously read by user in current session) then, there is a difference occurs between database record(that is updated by query based operation) and current session record, due to this, hibernate again runs update query to update session record during either flushing of session or on transaction completion.

To avoid the second execution executed by hibernate either during flushing of session or on transaction completion.

I wish it will help you.

Thanks

Mayur
  • 114
  • 6
  • 1
    "To avoid the second execution executed by hibernate either during flushing of session or on transaction completion." and ???? – Kartoch Jan 09 '15 at 15:47
1

I solve this probleme by adding : dynamic-update="true" in hbm.xml file

I found the solution here:

"The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement."

Kartoch
  • 7,610
  • 9
  • 40
  • 68
CHHIBI AMOR
  • 1,186
  • 2
  • 13
  • 27