简体   繁体   中英

Conditionally disable hibernate validation while executing hibernate query

I have a model which has some NOT NULL hibernate validations applied. I need to execute few DAO queries (create /update) such that the validations are relaxed only while executing those queries. I don't want to completely turn off the validations as there are other queries in my DAO layer which need them.

There was a way to circumvent the @NotNull check via native queries if your schema was not generated by Hibernate or Hibernate was not as smart as it ultimately is. You could try to insert / update your database rows natively without triggering the application layer validation but Hibernate is so kind to also generate the NOT NULL database constraint for fields annotated with @NotNull , what in the end is a great thing.

You should wonder why you have the need to execute queries which are not conform to the implemented validation restrictions, for sure the developer who added the constraint thought well about it's consequences. In general, the target should be to prevent inserting (avoidable) null values in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM