混合 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.
Is this possible?
Mort importantly, is this reasonable?
Are there better approaches?
How is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,但趋势更多的是集中化事物,而不是相反。
我并不因为实体类文件顶部有一块注释而烦恼。实际上,我喜欢将查询放在我认为属于它们的位置:实体旁边。 我还喜欢编译时检查(对实体名称、属性)以及在 Java 代码中编写查询时获得的代码完成(不确定我的 IDE 是否会使用 xml 映射来做到这一点)。换句话说,我不觉得有必要也不想外部化查询。
我相信使用注释是最佳实践1。
建议仅对特定数据库特定的本机 SQL 语句使用 XML 映射文件(当然,我忽略了无法注释的遗留代码的明显情况)。换句话说,使用注释,但尽可能使代码不受特定于供应商的内容的影响。
1 JPA 1.0 规范联合负责人 Mike Keith 涵盖了许多内容OTN 列“是否注释”中与 XML 元数据策略(XML 策略)与源内元数据策略(注释策略)相关的权衡。遗憾的是,我找不到他的出版物的非死链接。也许你会更幸运,在这种情况下,请阅读它。
Yes it is, but the trend is more to centralize things, not the inverse.
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 .
I believe that using annotations is the best practice1.
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 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.
我知道这有点晚了,但我遇到了这个,在我的项目中我一直在使用 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean (当然,在 Spring 容器中)。我真的很喜欢 JPA,我相信它会让生活变得更轻松,但我想在一个不使用 Hibernate、JPA、Spring 或任何好东西的项目中使用我的域类。我们决定,如果可能的话,最好让我们的一些域类不带有 Java 持久性注释。
我知道这是一件简单的事情,对很多人来说可能都是显而易见的,但我花了一段时间。下面是我的示例 POJO,请注意,我没有注释:
在 src/main/java/mypackage/domain/ 目录中(如果您使用的是 Maven),您应该放置一个漂亮的传统 XML 映射文件( Profile.hbm.xml):
如果您使用 Hibernate 4.0.0.CR3 应该没问题,那么 Spring 配置(我使用 3.0.6.RELEASE)可以看起来像典型的 JPA Hibernate 配置:
META- INF/persistence.xml 非常基本,为了完整起见,它是:
当然,虽然我使用 JPA,但映射配置是 Hibernate 特定的,因此我在这个项目中将自己与 Hibernate 绑定在一起,因为我正在使用遗留姐妹项目中的纯 JDBC 我不认为这是一个缺点。
I know this is a little late but I came across this and in my project I have been using org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean (in the Spring container, of course). I really like JPA and I am sure it will make life easier but I want to use my domain classes in a project that does not use Hibernate, JPA, Spring or any of that good stuff. We decided that it would be better to leave some of our domain classes free of Java Persistence annotations if possible.
I know this is a simple thing that is probably obvious to many but it took me a while. Below is my example POJO note that I have no annotations:
In the
src/main/java/mypackage/domain/
directory (if you are using Maven) you should put a nice, traditional XML mapping file (Profile.hbm.xml):Provided you are using Hibernate 4.0.0.CR3 that should be ok, The Spring configuration (I am using 3.0.6.RELEASE) can then look like a typical JPA Hibernate configuration:
META-INF/persistence.xml
is pretty basic and, for completeness here it is:Of course, although I am using JPA the mapping configuration is Hibernate specific and so I have tied myself to Hibernate in this project, since I am using pure JDBC in the legacy sister project I do not see it as such a drawback.
有可能,但我认为没有必要。我从事很多大型项目,其中一些实体附加了许多命名查询,我认为这不会使源代码变得混乱 - 毕竟所有查询都在类定义之前。使用注释的主要优点是您可以看到源代码中的所有内容。如果您在 xml 配置中提取了查询,则命名查询的存在不会立即可见,我认为这是一个缺点。我喜欢保持内容纯粹——要么仅设置 xml,要么仅设置注释。我通常在 persistence.xml 中保留 JPA 项目的唯一 xml 配置。
It's possible, but I don't think it's needed. I work on a lot of big projects with many named queries attached to some entities and I don't think that this clutters the source much - after all the queries are all before the class definition. The main advantage of using annotations is that you can see everything in the source. If you've extracted the queries in an xml config the presence of the named queries won't be immediately visible which I'd consider a drawback. I like to keep stuff pure - either xml only setup or only annotations setup. The only xml config I generally keep around on JPA projects in the persistence.xml.