使用 Hibernate 的 hbm2java 的最佳实践是什么?

发布于 2024-12-23 20:16:36 字数 456 浏览 4 评论 0原文

我正在使用 Hibernate、Maven 和 Eclipse(STS 构建)来构建项目。我使用 hbm.xml 文件来指定我的架构。我想使用 Hibernate 的 hbm2java 来生成我的模型类。我让它运行良好并生成了我想要的代码。

它可以从命令行完美运行,生成模型代码,然后按预期进行构建和测试。

然而,Eclipse 似乎无法处理它。它会定期“失去理智”,无法解析我的 DAO 类中引用的非常简单的导入和类,这些类是手工编码的。它找不到的是像 HibernateUtil 这样的类。具有讽刺意味的是,查找模型类似乎没有任何问题。

运行结束时,未解析的类位于 target/classes/blah-blah 文件夹中。所以它们显然被复制到了正确的位置。

在“持续集成”环境中,是否最好生成一次源代码,将它们提交到我的版本控制,然后禁用代码生成?或者是否可以每次生成代码,从而确保我在无需人工干预的情况下获取任何数据库更改?

I am using Hibernate, Maven, and Eclipse (STS build) to build a project. I'm using hbm.xml files to specify my schema. I want to use Hibernate's hbm2java to generate my model classes. I have it working well and generating the kind of code I want.

It runs perfectly from the command line, generating the model code and then building and testing as expected.

However, Eclipse seems unable to handle it. It will periodically "lose its mind" and be unable to resolve very simple imports and classes referenced in my DAO classes, which are hand-coded. The things it can't find are classes like HibernateUtil. Ironically, it appears to not have any trouble finding the model classes.

The unresolved classes are in target/classes/blah-blah folder at the end of the run. So they're apparently getting copied to the right place.

In a "continuous integration" environment, is it best to generate the sources once, commit them to my version control, and then disable code gen? Or is it possible to have the code generated each time, thus ensuring I pick up any database changes without human intervention?

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

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

发布评论

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

评论(2

一刻暧昧 2024-12-30 20:16:37

恕我直言,实体应该是应用程序的核心,并且应该谨慎设计、实施和记录。它们应该是对象,具有封装行为的方法。在我看来,让它们自动生成是荒谬的。

从一开始就生成它们可能是一个让您开始的选择,但是一旦生成它们,请手工制作它们并且不要再次生成它们。随着架构更改添加必要的属性和方法,并重构现有代码。

顺便说一句,我真的更喜欢使用注释进行映射,因为它不太冗长,不太容易出错,而且所有信息都在一个地方。

IMHO, entities should be the core of your application, and should be designed, implemented and documented with care. They're supposed to be objects, with methods encapsulating behavior. Having them autogenerated is an absurdity, IMO.

Generating them at the very beginning might be an option to get you started, but once they've been generated, hand-craft them and don't generate them again. Add necessary properties and methods as the schema changes, and refactor existing code.

BTW, I really prefer using annotations for the mapping, because it's less verbose, less error-prone, and all the information is in a single place.

失眠症患者 2024-12-30 20:16:37

试试这个:

从命令行遍历到项目的 pom.xml 所在的项目目录并运行:

mvn eclipse:clean eclipse:eclipse

如果它说无法找到插件 eclipse,则尝试:

mvn eclipse:install-plugin

首先,然后再次尝试上面的命令。

这样,所有 Maven 和项目依赖项也将在 Eclipse 级别得到解决。

如果这不是您想要的,请告诉我。

Try this:

From command line traverse to your project directory where the project's pom.xml is present and run:

mvn eclipse:clean eclipse:eclipse

If it says unable to find plugin eclipse then try:

mvn eclipse:install-plugin

First and then try the command above again.

In this way all the maven and project dependencies will be resolved at eclipse level also.

Let me know if this is not what you were looking for.

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