简体   繁体   中英

Should JPA Entities match database constraints and triggers?

Should JPA Entities match the constraints of the database they're mapping? What about database triggers used for data generation, should those be match as well?

Yes, you should model the constraints in your JPA model, as it is best to state those constraint explicit in your entities.

On one side because just of documentation purposes (many developers will look into your entity model while nearly none of them will look into the database model when using your beans).

On the other because you can catch wrong input as early as possible (potentially even in your UI), because many frameworks like JSF will look at them.

And you should try to move triggers from your database to your entities - to keep the application logic in one place and make such things explicit. But it makes no sense to double that logic - so if you need to keep these triggers in the database, don't add the same in the entities (but you should mention the triggers in the JavaDoc then).

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