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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Hibernate: Have a field that is not-peristed but can be pulled from DB? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Answer

You can add insertable and updatable false to the field

@Column(name="statusOrigin",insertable=false,updatable=false)
We are here to answer your question about Hibernate: Have a field that is not-peristed but can be pulled from DB? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji