休眠4.3.x - 加载所有实体注释的类 [英] hibernate 4.3.x - load all entity annotated classes

查看:95
本文介绍了休眠4.3.x - 加载所有实体注释的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我工作的项目中,我不使用Spring,只使用Hibernate。我不想将hbm.xml文件用于实体映射/描述/等。我只想使用注释。

如何让Hibernate从特定包中加载所有实体/表注释类?



我在网上搜索过,但我没有运气。此外,我没有找到有关最新的Hibernate版本(大多数是过时的文章/帖子/等)的信息。

编辑1:

http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/ html_single /#mapping

hibernate文档页面说明了这一点:


<可以使用三种方法定义对象/关系映射:

1)使用Java 5注释(通过Java Persistence 2注释)

2)使用JPA 2 XML部署描述符如第3章所述)
3)使用Hibernate传统XML文件方法hbm.xml

就是这样,我只想用1)和
SessionFactory / Session Hibernate API。


编辑2:

即使我的问题被标记为重复的,这不是,因为我不想使用JPA(和隐含的描述符),而只是从上面列出的那些方法中接近1)

解决方案

好的,这在Hibernate 4.3.x中是不可能的,而且似乎永远不可能。



另请参阅此问题/答案虽然这个帖子是旧的,但它是相当有效的):

在Hibernate中添加Annotated Class,方法是在某些包中添加所有类。 JAVA


$ b $ 1)在对此进行了一些研究之后,似乎普遍存在这样的误解: Configuration.addPackage 允许我们加载给定包中的所有实体类。这不是真的。通过查看休眠源,我发现它很困难,然后才发现上面的问题/答案确认了它。事实上,我不太清楚 addPackage 是干什么用的,但对我的情况来说似乎不是很有用。



2)似乎我们可以做的一件事就是为我们自己的每个注释实体类调用 Configuration.addAnnotatedClass 通过在编译时对这些类进行硬编码。或者...也可以使用反思 Guava 我们可以在运行时动态地从给定包中找到所有(即我们自己的)实体类,循环遍历它们,并且仍然调用 Configuration.addAnnotatedClass 。 Reflections的唯一问题是它带有很多自己的依赖关系。所以如果我们决定使用反射,我们必须为这个简单的东西添加9个新的JAR(真是痛苦)。使用Guava它有点简单,我们可以调用

ClassPath.from

Thread.currentThread()。getContextClassLoader()
)。
getTopLevelClasses(pckg)



如果有人有更好的方法 - 随时提供。
我会接受最好的答案,不一定是我的答案。


In a project which I work on I don't use Spring, I use Hibernate only. I don't want to use hbm.xml files for entity mappings/descriptions/etc. I want to use annotations only.

How do I tell Hibernate to load all Entity/Table annotated classes from certain package(s)?

I searched on the web but I have no luck. Also I don't find information about the latest Hibernate version (mostly outdated articles/posts/etc.).

Edit 1:

http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#mapping

The hibernate doc page says this:

Object/relational mappings can be defined in three approaches:
1) using Java 5 annotations (via the Java Persistence 2 annotations)
2) using JPA 2 XML deployment descriptors (described in chapter XXX)
3) using the Hibernate legacy XML files approach known as hbm.xml

So that's it, I just want to use 1) with the SessionFactory/Session Hibernate API.

Edit 2:

Even though my questions was marked as duplicate, it's not, because I don't want to use JPA (and the implied descriptors) but just approach 1) from those listed above.

解决方案

OK, this is not possible in Hibernate 4.3.x and was never possible it seems.

See also this question/answer (even though this post is old, it's quite valid):
Add Annotated Class in Hibernate by adding all classes in some package. JAVA

1) After I did some research on this, it seems it's a common misconception that Configuration.addPackage allows us to load all entity classes from a given package. It's not true. I found it the hard way by looking in the hibernate sources and only then I found the above SO question/answer which confirmed it. In fact I am not quite sure what addPackage does, but it doesn't seem very useful for my case.

2) Seems one thing we can do is to call Configuration.addAnnotatedClass for each of our own annotated entity classes e.g. by hard-coding those classes at compile time. Or ... alternatively by using Reflections or Guava we can find all (i.e. our own) entity classes from a given package dynamically at runtime, loop through them, and still call Configuration.addAnnotatedClass. The only problem with Reflections is that it comes with quite a few dependencies of its own. So we have to add 9 new JARs for this simple thing (what a pain) if we decide to use Reflections. With Guava it's somewhat easier, we can just call
ClassPath.from ( Thread.currentThread().getContextClassLoader() ). getTopLevelClasses(pckg).

If anyone has a better approach - feel free to provide it.
I will accept the best answer, not necessarily my answer.

这篇关于休眠4.3.x - 加载所有实体注释的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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