louis amoros
louis amoros

Reputation: 2546

Hibernate update specific entity field

Here is the entity model I want to save (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 entity:

Is there a way to tell Hibernate to ignore some fields for a specific method? Do I need to create two different specific dao methods? Do I need to keep updatable=false for a classic save method and create a custom one to update the notified attribute only? Best practices?

Upvotes: 4

Views: 3058

Answers (1)

gnos
gnos

Reputation: 855

I would suggest you to do it separately.

  • For the first update (all fields but notified) I would indeed use the updatable = false which tells hibernate not to include the field in the SQL statement. This way you just have to call tha save() method.

  • For the second update (only the notified field), Make a custom query :

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

    (Assuming the condition is an ID)

This should work the way you need. Hope that helps.

Upvotes: 4

Related Questions