英文:
How to set a calculated field from a stored procedure in a transient field of Entity?
问题
在数据库(Firebird)中有一张表,它与实体进行了映射。
一个存储过程与这张表一起工作,返回表中的字段值和计算得出的值。
对于实体中的这个计算值,我创建了一个瞬态变量。
我在 SqlResultSetMapping 中写下了从存储过程字段到实体字段的对应关系。
问题是瞬态字段中没有写入任何内容。
是否可以将计算值设置为瞬态字段?
@NamedStoredProcedureQuery(
name = Building.VR,
procedureName = "VALUATION_RESULTS_VC",
resultSetMappings = "Mapping",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "IP_ID", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_ID", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_CLASS", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_CRN_VC", type = Double.class)
}
)
@SqlResultSetMapping(
name = "Mapping",
entities = @EntityResult(
entityClass = Building.class,
fields = {
@FieldResult(name = "id", column = "P_ID"),
@FieldResult(name = "name", column = "P_CLASS"),
@FieldResult(name = "crn", column = "P_CRN_VC")
}
)
)
@Access(AccessType.FIELD)
@Entity
@Table(name = "main_tab")
public class Building extends BaseEntity {
public static final String VR = "Asset.VR";
@Transient
private Double crn;
public Double getCrn() {
return crn;
}
public void setCrn(Double crn) {
this.crn = crn;
}
}
@MappedSuperclass
public abstract class BaseEntity {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
}
英文:
There is a table in the database (Firebird), it is mapped to Entity.
A stored procedure works with this table, which returns both the field values from the table and the calculated value.
For this calculated value in Entity, I created a Transient variable.
I have SqlResultSetMapping in which I wrote the correspondence of fields from the stored procedure to the fields in Entity.
The problem is that nothing is written to the Transient field.
Is it possible to set calculated value to transient field?
@NamedStoredProcedureQuery(
name = Building.VR,
procedureName = "VALUATION_RESULTS_VC",
resultSetMappings = "Mapping",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "IP_ID", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_ID", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_CLASS", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "P_CRN_VC", type = Double.class)
}
)
@SqlResultSetMapping(
name = "Mapping",
entities = @EntityResult(
entityClass = Building.class,
fields = {
@FieldResult(name = "id", column = "P_ID"),
@FieldResult(name = "name", column = "P_CLASS"),
@FieldResult(name = "crn", column = "P_CRN_VC")
}
)
)
@Access(AccessType.FIELD)
@Entity
@Table(name = "main_tab")
public class Building extends BaseEntity {
public static final String VR = "Asset.VR";
@Transient <-if you remove Transient and create such a real field in the table, then everything works. But this field is calculated and it is not needed in the table.
private Double crn;
public Double getCrn() {
return crn;
}
public void setCrn(Double crn) {
this.crn = crn;
}
}
@MappedSuperclass
public abstract class BaseEntity {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
}
答案1
得分: 0
我找到了另一种类似的解决方案,就像下面这样:
在实体类中,使用@column(update=false,insertable=false)
定义属性,这仍然会在表中创建一个列,但作为只读列。可以使用这个列来作为派生列,从存储过程中获取数据。
英文:
I found one alternative solution like:
In entity class, define property with @column(update=false,insertable=false)
which still creates a column in table but acts a read only column. Use this column for derived column to fetch from stored procedure.
专注分享java语言的经验与见解,让所有开发者获益!
评论