Bob Zant Bob Zant - 1 year ago 247
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 ..

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download