JPA @Transient fields being cleared before @PreUpdate method is called

后端 未结 2 1984
别跟我提以往
别跟我提以往 2021-02-19 05:04

I have an User Entity class that I\'m trying to do password hashing for. I thought that the easiest way to do this would be to create a password field annotated with @Transient

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 05:39

    I solved this by setting updatable und insertable to false on the "transient" field, so in your case this would be:

    @Column(name = "password", insertable = false, updatable = false)
    private String password;
    

    Therefore a table @column is required (which is kind of ugly) but it will never be filled (which was important in my case for security reasons).

    I tested against Hibernate 4.3.4.Final and it worked for me. The field value was useable in my EntityLister @PrePersist and @PreUpdate methods but was not stored in the database.

    Hope that helps anybody having similar problems.

提交回复
热议问题