如何外部化 Hibernate 实体?

发布于 2024-11-18 14:35:26 字数 1855 浏览 2 评论 0原文

我正在开发一个基于 Java/Spring/Hibernate 的 Web 应用程序,在 Eclipse 中开发。

这个 Web 应用程序实际上是两个不同的项目,它们复制了几个 Hibernate 实体类以及它们各自的 Hibernate 存储库类。显然,最好将重复的代码移至外部 JAR 文件或其他文件中。

从 Hibernate 文档中,我知道我应该将实体类放入一个包含 persistence.xml 文件的新 JAVA 项目中。我还知道我可以将该项目导出到 JAR,并将其包含在我的主项目的类路径中。此外,我知道我应该从我的主项目中的 hibernate.cfg.xml 引用这个 JAR。

出于某种原因,对于我现在正在从事的项目来说,事情似乎并不那么简单。

以下是问题:

  1. 在保存共享代码的外部项目中,我也可以包含 Hibernate Repository 类吗?如果确实需要的话,应该如何在 persistence.xml 中注明这一点?

  2. 在我的外部 JAR 中,persistence.xml 可以有多小?例如,主项目已经配置了 hibernate 方言,那么我可以将其从外部 JAR 的 persistence.xml 中删除吗?

  3. 从 Hibernate 文档中,我知道我可以使用 hibernate.cfg.xml 中的 标记引用我的外部 JAR 文件。如果我的项目没有 hibernate.cfg.xml 怎么办?我们有一个 Spring 的配置 xml,它为 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 提供了一个 annotatedClasses 属性。我没有找到使用 AnnotationSessionFactoryBean 引用外部 JAR 文件的示例。

我尝试在这个网站上搜索,但还没有找到任何类似的东西来帮助我。如果这是重复的帖子,或者是一个愚蠢的明显问题或其他问题,我深表歉意。

提前致谢:)

编辑:

好的,所以看起来我最终需要知道的是,假设我已在类路径中包含 "MY_TEST.jar" ,并进一步假设 "MY_TEST .jar" 有一个带注释的 Hibernate 类:src.shared.myEntity.java

我如何使用下面的 Spring 配置摘录来引用这个实体类? (无论我尝试什么,都会不断收到 IllegalArgumentException: Cannot find class

<bean id="my.postgres.sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource"
        ref="my.postgres.dataSource" />

    <property name="annotatedClasses">
      <list>
        <value>src.app.EntityA</value>
        <value>src.app.EntityB</value>
      </list>
    </property>
    .........
</bean>

I'm working on a Java/Spring/Hibernate based web app, developed in Eclipse.

This web app is actually two different projects that duplicate a couple Hibernate entity classes, as well as their respective Hibernate repository classes. Obviously, it would be nice to take the duplicated code, and move it into an external JAR file or something.

From the Hibernate documentation, I know that I'm supposed to put the entity class in a new JAVA project that contains a persistence.xml file. I also know that I can then export this project to a JAR, and include it in the classpath of my main projects. Further, I know that I'm supposed to reference this JAR from my hibernate.cfg.xml in my main project.

For some reason, it doesn't seem to be quite so simple with the project I'm working on right now.

Here are the questions:

  1. in my external project that holds the shared code, can I include the Hibernate Repository class it it too? How should this be noted in the persistence.xml, if it indeed needs to be at all?

  2. In my external JAR, how minimal can the persistence.xml be? For instance, the main project already configures the hibernate dialect, so can I leave that out of this external JAR's persistence.xml?

  3. From the Hibernate docs, I know that I can reference my external JAR file using the <jar-file> tag in my hibernate.cfg.xml. So what if my project doesn't have a hibernate.cfg.xml? We have a configuration xml for Spring that gives an annotatedClasses property to the org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean. I'm not having much luck finding examples of referencing external JAR files with the AnnotationSessionFactoryBean.

I've tried searching around on this site, but haven't found anything similar enough to help me out yet. Apologies if this is a duplicate post, or a stupidly obvious question or something.

Thanks in advance :)

EDIT:

OK, so it looks like what I ultimately need to know is, supposing I've included "MY_TEST.jar" in my classpath, and further supposing "MY_TEST.jar" has one annotated Hibernate class: src.shared.myEntity.java

How would I go about referencing this entity class with the below Spring configuration excerpt? (keep getting IllegalArgumentException: Cannot find class pretty much no matter what I try)

<bean id="my.postgres.sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource"
        ref="my.postgres.dataSource" />

    <property name="annotatedClasses">
      <list>
        <value>src.app.EntityA</value>
        <value>src.app.EntityB</value>
      </list>
    </property>
    .........
</bean>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-11-25 14:35:26

Hibernate 没有存储库的概念。
春天确实如此。

Hibernate 不关心持久实体类在哪里。
它们必须在类路径中,
就这样。
无论他们是一体的,
二,
如果所有这些 jar 都在类路径中,或者三个 jar 并不重要。

按照您的意愿组织您的项目,并确保类路径中没有两个不同的休眠配置文件。
我将使用一个项目来保存所有常见实体以及它们关联的存储库,并让主项目定义休眠配置文件。

如果共享类的完全限定名称是 src.shared.myEntity,
那么 spring 配置应该有以下几行:

<property name="annotatedClasses">
  <list>
    ...
    <value>src.shared.myEntity</value>
  </list>
</property>

你的措辞令人困惑,
所以我会检查我认为显而易见的内容,
但也许并非如此。
类的完全限定名称由其完整的包名称,后跟类的名称组成,
没有 .class 或 .java 扩展名。
jar 必须包含 .class 文件,
不是 .java 文件。
jar 中目录的层次结构必须准确映射包的层次结构。
因此,如果您的实体看起来像这样:

package src.shared;

@Entity
public class myEntity {
    // ...
}

罐子应该有,
从根本上来说,
一个 src 目录,
包含一个共享目录,
包含 myEntity.class 文件。

PS:将 src 作为根包名称确实很奇怪。
为什么不尊重约定:com.yourcompany.yourproject...?

PPS:类名应始终以大写字母开头:
MyEntity 而不是 myEntity

Hibernate doesn't have the concept of a repository.
Spring does.

Hibernate doesn't care where the persistent entities classes are.
They must be in the classpath,
that's all.
Whether they're in one,
two,
or three jars doesn't matter if all these jars are in the classpath.

Organize your projects as you would like and just make sure you don't have two different hibernate configuration files in the classpath.
I would use a project to hold all the common entities and maybe their associated repositories and let the main project define the hibernate configuration file.

If the shared class fully qualified name is src.shared.myEntity,
then the spring config should have the following lines:

<property name="annotatedClasses">
  <list>
    ...
    <value>src.shared.myEntity</value>
  </list>
</property>

Your wording is confusing,
so I'll check what I considered obvious,
but which is perhaps not.
The fully qualified name of a class is composed of its full package name, followed by the name of the class,
without .class or .java extension.
The jar must contain the .class file,
not the .java file.
The hierarchy of the directories in the jar must exactly map the hierarchy of the packages.
So if your entity looks like this:

package src.shared;

@Entity
public class myEntity {
    // ...
}

The jar should have,
at its root,
a src directory,
containing a shared directory,
containing a myEntity.class file.

PS: having src as a root package name is really strange.
Why don't you respect the convention: com.yourcompany.yourproject...?

PPS: a class name should always start with a capital letter:
MyEntity and not myEntity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文