Mapping calculated properties with JPA

后端 未结 3 625
生来不讨喜
生来不讨喜 2021-01-04 11:35

Is there a way to map a calculated property using JPA?

Assuming I have an Invoice object with one or more InvoiceLineItems within it, I wan

3条回答
  •  太阳男子
    2021-01-04 12:24

    What you've described is not a calculated property in JPA sense. You're calculating it yourself within your method - just mark that method as @Transient and JPA will ignore it.

    If you truly need a calculated property (where "calculated" means "calculated via SQL expression"), you'll need to annotate it according to your JPA provider. For Hibernate you'd do that via @Formula annotation:

    @Formula("col1 * col2")
    public int getValue() {
     ...
    }
    

    Other providers may have their own ways to configure this; there's no JPA standard.

提交回复
热议问题