Hibernate:拥有一个不持久化但可以从数据库中提取的字段?

huangapple go评论79阅读模式
英文:

Hibernate: Have a field that is not-peristed but can be pulled from DB?

问题

我是下面的 Hibernate 实体:

@Entity(name = "status")
@Table(name = "status")
public class Status implements Serializable {

    @Id
    @JsonProperty
    @Column(name = "status_id")
    private Integer statusId;

    @JsonProperty
    @Column(name = "status_label")
    private String statusLabel;

    @JsonProperty
    @Transient
    private String statusOrigin;

}

statusOrigin 被标记为瞬态,因为它不是status 表中的列。

这对于仅使用 statusIdstatusLabel 字段创建对象正常工作,如预期所示。

然而,当我想通过连接查询将所有 3 个字段填充并将 Status 对象返回到前端时,由于 statusOrigin 是瞬态的,它不起作用。

我该如何做到以下几点:

  1. 保持创建功能不变
  2. 确保从连接查询的结果中填充 statusOrigin 字段并将其发送到 UI
英文:

I am the following Hibernate Entity:

@Entity(name = "status")
@Table(name = "status")
public class Status implements Serializable {

    @Id
    @JsonProperty
    @Column(name = "status_id")
    private Integer statusId;


    @JsonProperty
    @Column(name = "status_label")
    private String statusLabel;

    @JsonProperty
    @Transient
    private String statusOrigin;

}

statusOrigin is transient as it is not a column in the status table.

This works fine for creating the object with only statusId and statusLabel fields as expected.

However when I want to return the Status object to the front end with a join query which populates all 3 fields it does not work as statusOrigin is transient.

How can I do the following:

  1. keep the create functionality as it is
  2. Ensure that statusOrigin field is populated from the results of the join query and sent to UI

答案1

得分: 1

你可以在字段中添加"insertable"和"updatable"为"false"。

@Column(name="statusOrigin", insertable=false, updatable=false)
英文:

You can add insertable and updatable false to the field

@Column(name="statusOrigin",insertable=false,updatable=false)

huangapple
  • 本文由 发表于 2020年8月19日 00:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定