混合JPA注释和XML配置 [英] Mixing JPA annotations and XML configuration

查看:148
本文介绍了混合JPA注释和XML配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的(新)项目,其中我们已经注释了许多带有JPA映射的域类。现在是实现许多命名查询的时候了 - 一些实体可能有多达15-20个命名查询。我认为在注释中编写这些命名查询会混淆源文件,因此我考虑将这些放在XML映射文件中。

I have a fairly large (new) project in which we have annotated many domain classes with JPA mappings. Now it is time to implement many named queries -- some entities may have as many as 15-20 named queries. I am thinking that writing these named queries in annotations will clutter the source files and therefore am considering putting these in XML mapping files.

这是可能吗?

重要的是,这是否合理?

Mort importantly, is this reasonable?

有更好的方法吗?

这是如何完成的?

推荐答案


这是可能吗?

Is this possible?

是的,但趋势是更集中的事情,而不是逆。

Yes it is, but the trend is more to centralize things, not the inverse.


更重要的是,这是否合理?

More importantly, is this reasonable?

不会因为在我的实体类文件顶部有一个注释块而感到厌烦。其实, 喜欢在我认为他们属于我的查询:旁边的实体。 也喜欢编译时间检查(实体名称,属性)和代码完成时,我在Java代码中写入查询(不确定我的IDE将这样做与xml映射)。换句话说,不觉得有必要,也不想外部化查询。

I am not annoyed by a having a block of annotations at the top of my entities class files. Actually, I like to have my queries where I think they belong: next to entities. I also like the compile time checks (on entity names, attributes) and the code completion I get when writing queries in the Java code (not sure my IDE would do that with xml mappings). In other words, I don't feel the need and don't want to externalize queries .


有更好的方法吗?

Are there better approaches?

我相信使用注释是最好的做法 1

I believe that using annotations is the best practice1.


如何完成?

How is this done?

建议仅将XML映射文件用于对特定数据库特定的本机SQL语句(当然,我忽略了您无法注释的旧代码的明显情况)。换句话说,使用注释,但尽可能保持代码免受供应商特定的内容。

The recommendation is to use XML mapping files only for native SQL statements that are specific to a particular database (of course, I omit the obvious case of legacy code that you can't annotate). In other words, use annotations but keep the code as free from vendor-specific stuff as possible.

1 JPA 1.0规范共同领导 Mike Keith 在OTN列中介绍了与XML元数据策略(XML策略)相关的许多权衡取舍与源内容元数据策略(注释策略)To Annotate or不。可悲的是,我找不到他的出版物的非死链接。也许你会更幸运,在这种情况下,读它。

1 The JPA 1.0 specification co-lead Mike Keith covered many of the trade-offs associated with an XML metadata strategy (XML strategy) versus an in-source metadata strategy (annotations strategy) in the OTN column "To Annotate or Not". Sadly, I couldn't find a non-dead link to his publication. Maybe you'll be more lucky and in that case, read it.

这篇关于混合JPA注释和XML配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆