2

I am curious whether I can write something like this in JPA?

Or, Hibernate has such extension?

Or, @PostLoad has such equivalent?

Or, we must write separate DAO/Repository/Service?

@Entity
class Foo {

  @Id
  long id;

  @Basic
  String name;

  @Transit
  // Query("select address from Address a where a.username = this.name + '000' "
  String address;

}

Even more

@Entity
class Bar {

  @Id
  long id;

  @Basic
  String code;

  @Basic
  String name;

  @Transit
  // Query("select name from Bar b where b.code = substr(this.code, 0, 2) + '0000' "
  String provinceName;

  @Transit
  // Query("select name from Bar b where b.code = substr(this.code, 0, 4) + '00' "
  String cityName;

}
Raymond
  • 885
  • 10
  • 28

1 Answers1

2

Hibernate has the @Formula annotation that might just be what you're looking for. This is not standard JPA though. See this answer for details.

Community
  • 1
  • 1
Thomas Naskali
  • 551
  • 5
  • 19