java - 如何在实体的瞬态字段中设置存储过程中的计算字段?
问题描述
数据库中有一个表(Firebird),它映射到Entity。
存储过程使用此表,它返回表中的字段值和计算值。
对于 Entity 中的这个计算值,我创建了一个瞬态变量。
我有 SqlResultSetMapping,我在其中编写了从存储过程到 Entity 中的字段的字段的对应关系。
问题是 Transient 字段中没有写入任何内容。
是否可以将计算值设置为瞬态字段?
@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;
}
}
解决方案
我找到了一种替代解决方案,例如:
在实体类中,定义@column(update=false,insertable=false)
仍然在表中创建列但充当只读列的属性。将此列用于派生列以从存储过程中获取。
推荐阅读
- azure - 从服务总线队列中检索消息时出错
- jquery - jQuery ui datepicker 在 IOS 外部单击时不会消失
- maven - 在执行生命周期的 maven 命令中排除父级及其子级或模块
- android - Android IllegalAccessError 到我的类方法
- django - 赛普拉斯拖放测试
- javascript - 有没有办法可以在按钮中的 2 个 onclick 上分配 2 个单独的功能?
- spring - 为什么在关闭Eureka的情况下使用Zuul + Ribbon时路由会有延迟?
- asp.net - ASP.NET 页面和 VB.NET 代码从 IIS 8.5 (Win Server 2012 R2) 到 IIS 10 (Win Server 2016) 的应用程序迁移问题
- java - JPA Criteria API 递归获取延迟关联
- javascript - 在 VsCode 上为 3rd 方 API 扩展 Javascript 语言的指南