如何将计算字段添加到实体?
数据库表
table person {
id number,
first_name varchar,
last_name varchar,
...
}
Java实体
public class person {
BigDecimal id;
String firstName;
String lastName;
...//getters and setters
//what to add here???
public String getFullName() {
return firstName + " " + lastname;
}
}
我尝试添加@Transient,但转换为json时会忽略该字段。 我尝试将方法留在那里,抛出了一个异常,即setter丢失了。添加setter会引发另一个异常,即该字段在DB中不存在。 我尝试添加@Transient和@JsonPropert,但转换为json时会忽略该字段。 我尝试添加@Formula,但是hibernate(我认为)说它没有实现。
这个想法是让一个简单的计算字段被jpa / hibernate忽略,但是被jackson使用。 我怎么能做到这一点?
修改
示例全班
@Entity
@Table(name="FDF_PATIENT_COUNTIE")
@JsonIdentityInfo(generator = JSOGGenerator.class)
@JsonIgnoreProperties(ignoreUnknown = true)
@Audited
public class PatientCounty extends FgaBaseClass {
private static final long serialVersionUID = 1425318521043179798L;
private BigDecimal id;
private County FCounties;
private Patient patients;
public PatientCounty() {
}
public PatientCounty(County FCounties, Patient patients) {
this.FCounties = FCounties;
this.patients = patients;
}
@SequenceGenerator(name="generator", sequenceName="FDF_PATIENT_COUNTIE_SEQ")
@Id
@GeneratedValue(strategy=SEQUENCE, generator="generator")
@Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
public BigDecimal getId() {
return this.id;
}
public void setId(BigDecimal id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_F_COUNTIE")
public County getFCounties() {
return this.FCounties;
}
public void setFCounties(County FCounties) {
this.FCounties = FCounties;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID_FDF_PATIENT")
public Patient getPatients() {
return this.patients;
}
public void setPatients(Patient patients) {
this.patients = patients;
}
}