开发者

Reuse validation as DB constraints

开发者 https://www.devze.com 2023-03-20 12:34 出处:网络
I\'m using the Play! framework and discovered while writing tests that I can save an 开发者_JAVA百科invalid model instance, e.g. with invalid values for some instance variables. I guess that\'s expect

I'm using the Play! framework and discovered while writing tests that I can save an 开发者_JAVA百科invalid model instance, e.g. with invalid values for some instance variables. I guess that's expected behaviour to keep validation and persistence separated. But is there any way to reuse the validation annotations as database constraints?


I don't know anything about Play. Do you use JSR 303 validation annotations? According to the Play documentation, it can/does use Hibernate for persistence. As of Hibernate 3.5, Hibernate will include constraints expressed via JSR 303 annotations in the schema it generates.


No, the validation is not linked to database constraints. You have to add the constraints manually in the database via SQL, for example using the new evolutions framework of Play 1.2.1.

In your unit tests you must ensure the data you is correct. At any case you should only be testing logic in the Model, no dependencies between classes, just do some tests to check answer to unexpected parameters in places where they could be used but don't worry about instances saved with wrong data.

In your integration tests and selenium tests you should be able to use the Validation system provided by Play via @Valid on controller calls. Here is where you should ensure no objects with bad data are persisted, trying to add some.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号