简体   繁体   English

JPA实体是否应该匹配数据库约束和触发器?

[英]Should JPA Entities match database constraints and triggers?

Should JPA Entities match the constraints of the database they're mapping? JPA实体是否应该匹配他们映射的数据库的约束? 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. 是的,您应该在JPA模型中对约束进行建模,因为最好在实体中明确说明这些约束。

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). 一方面是因为文档目的(许多开发人员会调查您的实体模型,而在使用您的bean时几乎没有人会查看数据库模型)。

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. 另一方面,因为您可以尽早捕获错误的输入(甚至可能在您的UI中),因为像JSF这样的许多框架都会查看它们。

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). 但是将逻辑加倍是没有意义的 - 所以如果你需要在数据库中保留这些触发器,不要在实体中添加相同的东西(但是你应该在JavaDoc中提到触发器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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