How to @Validate unique username in spring?

后端 未结 3 1133

Let\'s assume that I have a form where I might submit username(@NaturalId) and password for a new user.

I would like to add the user with a

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 23:04

    JSR-303 does not support what you want (something like a @Unique constraint). You have to write you own validator. How can this be done is explained here: https://community.jboss.org/wiki/AccessingtheHibernateSessionwithinaConstraintValidator

    But before doing that make sure you read this answer: https://stackoverflow.com/a/3499111/1981720

    And this sentence from the previous article:

    The reason @Unique is not part of the built-in constraints is the fact that accessing the Session/EntityManager during a valiation is opening yourself up for potenital phantom reads.

提交回复
热议问题