迁移到 ORM

发布于 2024-08-22 12:43:30 字数 65 浏览 4 评论 0原文

逐步来说,将 Spring 和 Hibernate 集成到不使用 ORM 的现有 JSF 应用程序中的好方法是什么?

Stepwise, what would be a good way of integrating Spring and Hibernate into an existing JSF application that doesn't use ORM?

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

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

发布评论

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

评论(3

疧_╮線 2024-08-29 12:43:30

1)您需要先设计领域模型,然后将其映射到数据库。您可以使用 hibernate 逆向工程工具来实现此目的。
2)其他选项是将当前对象(DTO)手动映射到数据库表。由于您使用 JSF,我假设您当前拥有一些对象。
3) 围绕对象设计服务层。

春天 -
1) 可以使用Spring提供hibernate模板,通过bean提供常用服务。
2)您可以在Spring中管理事务。

1) You would need to design the domain model first and then map it to the database. You could use hibernate reverse engineering tools for this.
2) Other option is to manually map your current objects(DTO) to database tables. Since you use JSF, I assume you'd be having some objects currently.
3) Design the Service Layer around the objects.

Spring -
1) You could use Spring to provide hibernate template, provide common services through bean.
2) You can manage the transaction in Spring.

秉烛思 2024-08-29 12:43:30
  1. 我建议首先编写测试来检查您以前的持久机制的代码。此代码可用于检查 ORM 集成的正确行为。
  2. 正如其他答案所提到的,通过接口定义清晰的 DAO 有助于绑定 DAO 代码。
  3. 首先映射域对象,然后编写 DAO,然后编写服务对象(通过将其包含在事务中来处理大型原子操作套件)。
  4. 使用与供应商无关的持久性机制(JPA 是最好且唯一的选择)。
  5. 从一种数据库开始,并在整个迁移过程中坚持使用它。在非常罕见的情况下,您可能会遇到数据库之间的细微差异,这可能很难解决,特别是如果您是初学者。
  6. 开始时,使用自动生成数据库(hibernate子系统的generateDdl),然后,当事情开始稳定时,强制@Table和@Column注释来修复每列的名称。此时,编写一个 SQL 脚本来生成带有空表的数据库。原因是要修复您的体系结构并确保您正在控制数据库组织。

如果您认真对待 ORM,请查看 Christian Bauer 的 Java Persistence With Hibernate(Manning 出版社),这是关于 Java 上的 hibernate 和 JPA 的“圣经”。

  1. I would recommend first to write tests to check your code of your previous persistent mechanism. This code could be used to check the correct behavior of our ORM integration.
  2. As mentioned by other answers, having a clear DAO defined by interface helps to bound the DAO code.
  3. Map the domain objects first, then write your DAO, then your service objects (which take care of large atomic suite of operations by enclosing its in a transaction).
  4. Use persistence mechanism which is vendor-agnostic (JPA is the good and only choice).
  5. Start with one kind of database and stick with it during all the migration. In very uncommon cases, you can meet subtle differences between databases which could be very hard to solve, especially if you're a beginner.
  6. When starting, use automatic generation of database (generateDdl for hibernate subsystem) and then, when things starts to be stabilized, force @Table and @Column annotations to fix name of each column. At this point, write a SQL script which generate the database with empty tables. The reason if to fix your architecture and be sure you're controlling the database organization.

If you're serious about ORM, please look at Java Persistence With Hibernate of Christian Bauer (Manning publications), this is "the bible" about hibernate and JPA on Java.

百合的盛世恋 2024-08-29 12:43:30

如果您正确编写了 Spring,您应该拥有 DAO/存储库接口。如果是这种情况,您所要做的就是使用 Hibernate 编写该接口的新实现,对其进行测试,然后更改 Spring 配置以注入它。

ORM 假定您有一个对象模型可以映射到关系模式。如果你不这样做,我建议你不要使用 ORM。在这种情况下,最好使用 iBatis 或 JDBC。

If you've written Spring properly, you should have DAO/repository interfaces. If that's the case, all you have to do is write a new implementation of that interface using Hibernate, test it, and change your Spring configuration to inject it.

ORM presumes that you have an object model in place to map to a relational schema. If you don't, I would advise against using ORM. Better to use iBatis or JDBC in that case.

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