Hibernate updates a specific field of an object

Here is the entity model I want to keep (create / update):

@Entity
@Table(name = "EVENT")
public class EventEntity {

    @Column(name = "NATURE")
    private String nature;

    @Column(name = "END_DATE")
    private LocalDate endDate;

    @Column(name = "NOTIFIED", insertable = false)
    @NotNull
    private Boolean notified;

    // Getter - Setter - Builder
}

      

There are two ways to update this object:

  • The first one to update all attributes except the attribute notified

  • Second to update the attribute notified

Is there a way to tell Hibernate to ignore some fields for a specific method? Do I need to create two different concrete dao methods? Do I need to save updatable=false

for the classic method save

and create a custom one to update the attribute notified

? Best practics?

+3


source to share


1 answer


I suggest you do this separately.

  • For the first update (all fields but notified) I would actually use updatable = false

    which tells hibernate not to include this field in the SQL statement. Thus, you just need to call the save () method.

  • For the second update (only the notification box), make your own query:

    @Modifying
    @Query("UPDATE EventEntity e SET e.notified = ?2 WHERE e.id = ?1")
    @Transactional
    void customUpdate(UUID itemId, boolean notified);
    
          

    (Assuming this condition is an identifier)



This should work the way you want it to. Hope it helps.

+3


source







All Articles