# Update auditing fields of entity when @OneToMany relation updates

**URL:** <https://discourse.hibernate.org/t/update-auditing-fields-of-entity-when-onetomany-relation-updates/9720>\
**Category:** Hibernate ORM\
**Created:** [June 10, 2024, 9:20am UTC](https://discourse.hibernate.org/t/update-auditing-fields-of-entity-when-onetomany-relation-updates/9720 "2024-06-10T09:20:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![azizairo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/azizairo/32/2534_2.png) [@azizairo](https://discourse.hibernate.org/u/azizairo)\
**Post date:** [June 10, 2024, 9:20am UTC](https://discourse.hibernate.org/t/update-auditing-fields-of-entity-when-onetomany-relation-updates/9720/1 "2024-06-10T09:20:29Z")

</div>

Hi, I’m trying to update lastModifedAt and lastModifiedBy fields when trying to add or update child from parent entity, but it does not trigger modification of parrent. How can i achive this?

My Entities:

```java
public class Term {

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @CreatedDate
    @Column(name = "created_at", nullable = false)
    private Instant createdAt;

    @CreatedBy
    @Column(name = "created_by", nullable = false)
    private String createdBy;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private Instant updatedAt;

    @LastModifiedBy
    @Column(name = "updated_by", nullable = false)
    private String updatedBy;

    @OneToMany(mappedBy = "term", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private Set<TermContent> contents = new HashSet<>();

    public void addContent(TermContent content) {
        contents.add(content);
        content.setTerm(this);
    }
}

```

And how i am adding new content to term:

```java
public TermContent addContent(UUID termId, AddContentRequest request) {

        Term term = termRepository.findEagerlyById(termId)
                .orElseThrow(() -> new EntityNotFoundException(Term.class, termId));

        TermContent termContent = new TermContent();
        termContent.setTerm(term);
        termContent.setName(request.getName());
        termContent.setLanguage(languageService.findById(request.getLanguageId()));
        termContent = termContentRepository.save(termContent);

        term.addContent(termContent);
        termRepository.save(term);

        return termContent;
    }

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 11, 2024, 11:11pm UTC](https://discourse.hibernate.org/t/update-auditing-fields-of-entity-when-onetomany-relation-updates/9720/2 "2024-06-11T23:11:25Z")

</div>

This was recently asked in [the chat](https://hibernate.zulipchat.com/#narrow/stream/132096-hibernate-user/topic/Modification.20of.20collection.20notification).

---

<div class="post-metadata">

**Author:** ![azizairo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/azizairo/32/2534_2.png) [@azizairo](https://discourse.hibernate.org/u/azizairo)\
**Post date:** [June 17, 2024, 9:47am UTC](https://discourse.hibernate.org/t/update-auditing-fields-of-entity-when-onetomany-relation-updates/9720/4 "2024-06-17T09:47:11Z")

</div>

Thanks, I’ll check it!
