简体   繁体   English

JPA:我应该使用orm.xml清理我的实体类吗?

[英]JPA: Should I clean up my entity classes using orm.xml?

I currently use JPA annotations only, but I really don't like that I'm polluting my entity classes with so many ORM details which really aren't relevant to their behavior (eg table name, id generation strategies, join columns...). 我目前只使用JPA注释,但我真的不喜欢我用这么多ORM细节污染我的实体类,这些细节实际上与它们的行为无关(例如表名,id生成策略,连接列...... )。

I see that DataNucleus recommends putting ORM-related annotations in XML instead (those colored in pink), but I haven't seen any other implementations recommend this and JPA doesn't seem to separate annotations into these two groups (I think JDO does). 我看到DataNucleus建议在XML中添加与ORM相关的注释 (粉红色的那些),但我没有看到任何其他实现推荐这个,JPA似乎没有将注释分成这两个组(我认为JDO确实如此) 。

Is anyone using annotations+orm.xml in this way, and what are your experiences? 是否有人以这种方式使用注释+ orm.xml,您有什么经历?

Will it remove some of the pollution from my entity classes, or will I run into problems? 它会从我的实体课程中消除一些污染,还是会遇到问题?

The biggest issue we've faced on a semi-regular basis is that if you want to change your persistence mapping in any way using annotations alone you need to recompile and redeploy. 我们在半定期基础上遇到的最大问题是,如果您想单独使用注释以任何方式更改持久性映射,则需要重新编译和重新部署。

Using orm.xml affords you a degree of abstraction which can make reconfiguration a little more straight forward, and achievable with technically the same code base (eg you're sure that a line of code hasn't snuck into what you're recompiling/redeploying). 使用orm.xml为您提供了一定程度的抽象,可以使重新配置更直接,并且可以通过技术上相同的代码库实现(例如,您确定一行代码没有隐藏到您正在重新编译的内容/重新部署)。

You can use both annotations and configuration - which is the environment I'm now working with - classes are annotated with functional type persistence metadata (ie foreign keys, joins, etc - things which have a code level representation) while irrelevant information (ie table/column names) are stored in configuration files. 您可以使用注释配置 - 这是我正在使用的环境 - 类使用功能类型持久性元数据(即外键,连接等 - 具有代码级别表示的事物)注释,而无关信息(即表) /列名称)存储在配置文件中。

We're still trying to develop some clear heuristics around when we use one configuration mechanism over another, but we're getting there. 当我们使用一种配置机制而不是另一种配置机制时,我们仍在尝试开发一些明确的启发式方法,但我们已经到了那里。

JPA entities being just Java Beans (classes defining getters and setters) with optional supporting code (constructors, hashCode, equals, named queries, copy methods, what else?) could hardly be considered polluted even with all types of JPA annotations included. JPA实体只是Java Beans(定义getter和setter的类),带有可选的支持代码(构造函数,hashCode,equals,命名查询,复制方法,还有什么?),即使包含所有类型的JPA注释,也很难被认为是污染的。

The real purpose of splitting metadata between Java annotations and xml would be simplification and optimization of deployment policies. 在Java注释和xml之间拆分元数据的真正目的是简化和优化部署策略。 The price that you will incur is two-fold: 您将承担的价格是双重的:

  1. enforcement of development policies regarding what metadata belongs to which place and 关于什么元数据属于哪个地方的发展政策的执行
  2. cross referencing java and xml when creating and especially maintaining metadata (more or less but always inconvenience). 在创建和特别维护元数据时交叉引用java和xml(或多或少但总是不方便)。

Both are rather serious considerations when working in average to large size development team. 在平均大型开发团队工作时,两者都是相当严肃的考虑因素。

If recompiling for database changes presents a significant challenge in your deployment process then it sounds as a reasonable approach. 如果重新编译数据库更改会给您的部署过程带来重大挑战,那么这听起来是一种合理的方法。 But the price will be more complex development environment and process and maintenance policies. 但价格将是更复杂的开发环境和流程维护政策。

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

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