Rpj
Rpj

Reputation: 6110

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

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;
  }

}

Upvotes: 0

Views: 242

Answers (1)

Uladzislau Kaminski
Uladzislau Kaminski

Reputation: 2255

Just add this value as a constructor parameter.

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

Upvotes: 1

Related Questions