Answer the question
In order to leave comments, you need to log in
Hibernate how to initialize calculated field in Entity?
Hello!
I have an @Entity class. The fields in it are filled from the database using Hibernate.
I want to add a field to this class that is not in the database and is calculated based on other fields. Accordingly, I need all the fields to be picked up from the base before I start calculating my value. How to do it? Maybe there is some annotation on this account, or some other technique?
PS: so far I have only thought of making a wrapper over the @Entity class, in which I will receive the object and set the field (or make the field in the wrapper itself). But I don't really like this way...
Answer the question
In order to leave comments, you need to log in
Good afternoon!
I want to add a field to this class that is not in the database
@Transient
@Transient
private long calc;
...which is calculated based on other fields.
@Formula
@Formula
create a method with an annotation @PostConstruct
and then initialize your field with the desired value
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question