简体   繁体   English

hibernate 4.3.x - 加载所有实体注释类

[英]hibernate 4.3.x - load all entity annotated classes

In a project which I work on I don't use Spring, I use Hibernate only. 在我工作的项目中,我不使用Spring,我只使用Hibernate。 I don't want to use hbm.xml files for entity mappings/descriptions/etc. 我不想将hbm.xml文件用于实体映射/描述/等。 I want to use annotations only. 我只想使用注释。

How do I tell Hibernate to load all Entity/Table annotated classes from certain package(s)? 如何告诉Hibernate从某些软件包加载所有Entity/Table注释类?

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.). 另外,我找不到有关最新Hibernate版本的信息(主要是过时的文章/帖子等)。

Edit 1: 编辑1:

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

The hibernate doc page says this: hibernate doc页面说明了这一点:

Object/relational mappings can be defined in three approaches: 对象/关系映射可以用三种方法定义:
1) using Java 5 annotations (via the Java Persistence 2 annotations) 1)使用Java 5注释(通过Java Persistence 2注释)
2) using JPA 2 XML deployment descriptors (described in chapter XXX) 2)使用JPA 2 XML部署描述符(在第XXX章中描述)
3) using the Hibernate legacy XML files approach known as hbm.xml 3)使用称为hbm.xml的Hibernate遗留XML文件方法

So that's it, I just want to use 1) with the SessionFactory/Session Hibernate API. 就这样,我只想使用1)与SessionFactory / Session Hibernate API。

Edit 2: 编辑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. 即使我的问题被标记为重复,但事实并非如此,因为我不想使用JPA(以及隐含的描述符),而只是使用上面列出的那些方法1)。

OK, this is not possible in Hibernate 4.3.x and was never possible it seems. 好吧,这在Hibernate 4.3.x中是不可能的,而且似乎永远不可能。

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. 通过在某个包中添加所有类,在Hibernate中添加带注释的类。 JAVA 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. 1)在我对此做了一些研究之后,似乎是一种常见的误解,即Configuration.addPackage允许我们从给定的包中加载所有实体类。 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. 通过查看hibernate源我发现它很难,然后才发现上面的SO问题/答案证实了这一点。 In fact I am not quite sure what addPackage does, but it doesn't seem very useful for my case. 事实上,我不太确定addPackage是做什么的,但它对我的情况似乎没有用。

2) Seems one thing we can do is to call Configuration.addAnnotatedClass for each of our own annotated entity classes eg by hard-coding those classes at compile time. 2)似乎我们可以做的一件事是为我们自己的每个注释实体类调用Configuration.addAnnotatedClass ,例如通过在编译时对这些类进行硬编码。 Or ... alternatively by using Reflections or Guava we can find all (ie our own) entity classes from a given package dynamically at runtime, loop through them, and still call Configuration.addAnnotatedClass . 或者......或者通过使用ReflectionsGuava,我们可以在运行时动态地从给定包中找到所有(即我们自己的)实体类,循环它们,并仍然调用Configuration.addAnnotatedClass The only problem with Reflections is that it comes with quite a few dependencies of its own. Reflection的唯一问题是它带有很多自己的依赖。 So we have to add 9 new JARs for this simple thing (what a pain) if we decide to use Reflections. 因此,如果我们决定使用Reflections,我们必须为这个简单的事情添加9个新的JAR(多么痛苦)。 With Guava it's somewhat easier, we can just call 使用番石榴它更容易,我们可以打电话
ClassPath.from ( Thread.currentThread().getContextClassLoader() ). getTopLevelClasses(pckg) ClassPath.from ( Thread.currentThread().getContextClassLoader() ). getTopLevelClasses(pckg) . 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. 我会接受最好的答案,不一定是我的答案。

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

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