javasqlhibernateentitytransient

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


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

Solution

  • You can add insertable and updatable false to the field

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