Bob Zant Bob Zant - 8 months ago 87
Java Question

JPA calculated field from other fields

I am working on a system where I need to calculate points based on other properties in the same entity, i.e:

@Column(name = "distance")
private Integer distance;
@Column(name = "speed")
private Integer speed;
@Column(name = "calories")
private Integer calories ??

I need to calculate the calories based on speed and distance and persist this value ..


you don't need to persist them - because you can get the value of sum anyway any time. Why are you trying to save it? @Transient

public Integer getSum(){
return (distance + speed) * calories 

Use @Transient to be able not to save this value in DB.