I am pretty new in Hibernate and I have the following doubt.
Into a database table named TWP1007_PROGETTO I have this row (but the content is not important at this time):
PRG_PRO COD_MEC_ATT DAT_ANN_SCO_ATT COD_MEC_INI_PRO COD_PRO IMP_FIN COD_TIP_STA FLG_TIP_PRG PRG_GRA_WIF PRG_GRA_PNS FLG_PRE_FIR_DIR FLG_PRE_FIR_REV DAT_INS_REN DESC_NOM_FIL_REN OGG_FIL_REN DAT_VAL DES_NOT_VAL DAT_TRA_UFF_LIQ IMP_TOT_SPE IMP_SAL DES_NOM_UTE_VAL DES_NOM_DIR_REN DAT_ORA_ULT_MOV COD_PGM_ULT_MOV COD_UTE_ULT_MOV
2 AGIS018009 201516 AGIS018009 Progetto Wifi 7980,00 4 W 2 null 12/10/2015 00:00:00 Rimesso in lavorazione user.name
Then, into a Spring MVC service class, I have create this method to update a record of this table:
public void rimettiInLavorazioneProgetto(Twp1007Progetto progetto) {
progettoRepo.save(progetto);
}
Where (Twp1007Progetto progetto is the object that map the TWP1007_PROGETTO table).
So as you can see in the previous method I use the save()
hibernate method.
It works and the record is correctly updated but it seems strange because from what I have understand the save()
method have to create a new record whereas the update()
method have to update an existing record.
So why it works? Why it correctly update an existing record?
Yes. Save()
method do create new record if and only if your Object don't have an generated identifier
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.)
If you have already a generated identifier associated with your Object, it just saves to that row.