Spring Data JPA - how to set transient fields after retrieval

前端 未结 3 868
遇见更好的自我
遇见更好的自我 2021-01-22 16:41

After an entity is fetched using the JpaRepository methods of Spring Data JPA, e.g. findOne, findBy..., etc., I was wondering what would b

3条回答
  •  温柔的废话
    2021-01-22 17:22

    You can use @Access(AccessType.Property) for setting transient fields ideally.

    You have to mention @Access(AccessType.Property) on the setter method normally and then you can set value of that transient field in that setter method.

    Alternatively in case of repositories , you can very well write a JPQL something like this.

    @Query("SELECT new Hello(a.x*a.y,a.x,a.y)FROM Hello a WHERE t.value = :value")

    and then in the constructor set the value of this transient variable with a.x*a.y

提交回复
热议问题