Issue
Image that this is an entity with full structure well defined
public class Entyty{
//genarate by sequence
private Long id;
//value coming from request
private String value;
//I need concatenate this value with id + value before to save in db
private String composeValue;
}
someone knows how to do it?
Solution
You can't do it with the normal JPA process.
The reason is rather obvious: The id only gets generated when the entity gets saved.
So you either have to make your requirement more lenient or manually assign the id. It might still come from a sequence if that is important to you.
So here are some variants that might be helpfull:
Generate the id manually. You could select the next value for a sequence from the database using a normal SQL query and use it to set the id of any new entity. You can then use normal java code to concatenate it with another property.
You can have a getter only that will take the id and the other property concatenate them and return the result. Once the property and id are set the getter will return the correct result. Before that not so much.
You can populate the field in the database using a trigger. If you want you may select the extra field or just keep it in the database.
Instead of using a trigger you can use a view and have the view assemble the extra field. If you use it in where clauses or similar you may put a function based index on it.
Answered By - Jens Schauder
Answer Checked By - Mary Flores (JavaFixing Volunteer)