Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:08:43+00:00 2026-06-14T18:08:43+00:00

We ran across a problem where we have to maintain a date field –

  • 0

We ran across a problem where we have to maintain a date field – and preferably persist its value – where the date field is dependent on other entities’ state. What is the correct way to address this problem with JPA2 + Hibernate + Spring?

Basically, we have three entities, let’s call them Parent, Child, and Conf. The dependentDate is calculated by complex business rules that depend on other entities’ state. Specifically, it should be Parent#date minus Conf#delay skipping all days that are weekends or holidays (persistent data).

The problem is that whenever the Parent#date changes, all the children’s dependentDates should be recalculated. This should happen also whenever the Child object’s conf is changed. Currently, there is a service method for calculating the new date based on the Child object’s relations (let’s call it calculateDate()).

We are faced with the problem of where the field’s value should be calculated. I came up with three possible solutions. Which of these – or possibly something else – is the preferred way of updating entities’ fields that depend on other entities’ state?

  1. A JPA EntityListener
    • this is discouraged in the JPA2 spec, since the EntityListeners should not access other entities
    • the field would always be correct
  2. An Spring AOP aspect that updates
    • the logic is hidden in some other class and is not easily seen
    • a risk that the pointcut does not catch all persist/update events
  3. Every developer for himself
    • always, when updating an instance of either Parent or Child class, the developer must remember to call the service’s calculateDate() method
    • should this be in dao or service layer?
    • risk of the developer forgetting to add the call, resulting in erroneous state

The following example shows the entity relations with omitted annotation configuration.

The entities

public class Parent {
     LocalDate date;
     Set<Child> children;     // one-to-many
}

public class Conf {
     int delay;
}

public class Child {
     LocalDate dependentDate;
     Conf conf;               // many-to-one
     Parent parent;           // many-to-one
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:08:44+00:00Added an answer on June 14, 2026 at 6:08 pm

    Complex business rules that depend on several entities’ state belong to the service layer. There shouldn’t be so many places in the application where the parent’s date and the child’s conf are changed, so you shouldn’t have so many places where the new date calculation must be done.

    If you really have so many places where this is done, you could make it clear that the date must be recomputed by passing a ChildDateUpdater instance (ChildDateUpdater being an interface) to the setter of the parent date and the setter of the child conf. This would make it obvious that something must be done every time these two fields are changed:

    In Parent:

    public void setDate(Date date, ChildDateUpdater childDateUpdater) {
        this.date = date;
        for (Child child : children) {
            childDateUpdater.updateChildDate(child);
        }
    }
    

    In Child:

    public void setConf(Conf conf, ChildDateUpdater childDateUpdater) {
        this.conf = conf;
        childDateUpdater.updateChildDate(this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just ran across the problem described below. If Console.TreatControlCAsInput = true;, you have to
As a Matlab user transitioning to R, I have ran across the problem of
This one really has me stumped. I have not ran across this problem on
Interesting problem I ran across which makes total sense. I have a generic method
I ran across a problem with a SQL statement today that I was able
I'm quite new to C++ and I've ran across a problem. I'm trying to
In answering this code golf question , I ran across a problem in my
I ran across an interesting issue today. We have an application that utilizes Zend
I just ran across a problem that took a little while to figure out
I have this code below. I randomly ran across that it will work if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.