How to pre-fill a transient value on a JPA Entity

Viewed 26

How to pre-fill a transient attribute (say accountId) on a JPA Entity. I am trying to set accountId using a constant fetched from the application environment (say appAccountId). Is this the right way to fetch this information, when a select is run on the Entity?

@Entity
public class Foo {

  @Value("${app.account.id}")
  private String appAccountId;

  private String transient accountId;

  @Postload 
  public void postLoad() {
      accountId = appAccountId;
  }

}
1 Answers

Just add this value as a constructor parameter.

public class Foo {
    public Foo(@Value("${app.account.id}") String accountId) {
        this.accountId = accountId
    }
....
Related