首页 > 解决方案 > 如何在实体的瞬态字段中设置存储过程中的计算字段?

问题描述

数据库中有一个表(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;
    }
}

标签: javaspringhibernatejpa

解决方案


我找到了一种替代解决方案,例如:

在实体类中,定义@column(update=false,insertable=false)仍然在表中创建列但充当只读列的属性。将此列用于派生列以从存储过程中获取。


推荐阅读