-1

I using spring boot and I have an Entity that store path of a URL and because of domain address could be changed in the future, I didn't store full URL into the database. Here is my entity:

@Entity
public class TestModel
{
    private String urlPath;

    @Transient
    private String fullUrl;

    //getters and setters...
}

I need to concat domain address with urlPath and store its value into fullUrl field during serialization model.
I know I can do this in getter method of the fullUrl field, But domain address should be read from the configuration and model should not have anything about the business logic.
what is the best practice to implement it?

Ali
  • 443
  • 5
  • 22

1 Answers1

0

Use @PrePersist, @PreUpdate annotations. See for example https://www.baeldung.com/database-auditing-jpa

If your business logic to modify the value lives in an external bean you can use @EntityListeners(MyEntityListener.class) and use @PrePersist, @PreUpdate annotations there.

As those entitylisteners classes are not beans but POJOs to autowire external beans into them you may use one of the strategies mentioned here: Injecting a Spring dependency into a JPA EntityListener to get your business logic beans autowired into those entitylisteners.

TomB
  • 1,010
  • 10
  • 14