英文:
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
表中的列。
这对于仅使用 statusId
和 statusLabel
字段创建对象正常工作,如预期所示。
然而,当我想通过连接查询将所有 3 个字段填充并将 Status 对象返回到前端时,由于 statusOrigin
是瞬态的,它不起作用。
我该如何做到以下几点:
- 保持创建功能不变
- 确保从连接查询的结果中填充 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:
- keep the create functionality as it is
- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论