我们什么时候真的需要在 Java 代码中使用 hibernate?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12189251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When do we really need to use hibernate for our Java code?
提问by bread butter
As a beginner to Java-database projects, how does one decide when one should use Hibernate in Java code instead of simple jdbc?
作为 Java 数据库项目的初学者,如何决定何时应该在 Java 代码中使用 Hibernate 而不是简单的 jdbc?
采纳答案by techuser soma
Use hibernate if 1 to 4 are satisfied
如果满足 1 到 4,则使用休眠
- If you are designing the database schema and your domain objects afresh.
- You envision that your domain objects and your schema will be in sync, for example an automobile vendor application where your vehicle objects are likely to be close to your table structures. Just an example.
- You really want to use an ORM and tie your objects closely to schema.
- Want to use a ORM framework to handle connections etc.
- 如果您正在重新设计数据库架构和域对象。
- 您设想您的域对象和您的模式将保持同步,例如汽车供应商应用程序,其中您的车辆对象可能与您的表结构接近。只是一个例子。
- 您确实想使用 ORM 并将您的对象与模式紧密联系起来。
- 想使用 ORM 框架来处理连接等。
Else
别的
I would recommend ibatis. This is still an ORM framework but leaves flexibility in handling schema and domain objects separatley. It offers more flexibility in handling queries etc.
我会推荐ibatis。这仍然是一个 ORM 框架,但在单独处理模式和域对象方面留下了灵活性。它在处理查询等方面提供了更大的灵活性。
Spring jdbc also has good features offering flexibility. But i feel it still not near to an ORM. But spring does offer templates to handle orms like hibernate and ibatis.
Spring jdbc 还具有提供灵活性的良好特性。但我觉得它仍然不接近 ORM。但是 spring 确实提供了处理像 hibernate 和 ibatis 这样的 orm 的模板。
回答by jtahlborn
Basically, if your application needs to store some data, you will probably want to use an ORM (e.g. you built some business app for your company and you need to track users and accounts). If your application isyour data, then you will probably want to use JDBC (e.g. you built a data warehouse for your company).
基本上,如果您的应用程序需要存储一些数据,您可能需要使用 ORM(例如,您为公司构建了一些业务应用程序,并且需要跟踪用户和帐户)。如果您的应用程序是您的数据,那么您可能希望使用 JDBC(例如,您为您的公司构建了一个数据仓库)。
as an additional note, it's perfectly reasonable for an application to use bothORM and JDBC. if, for instance, you were building a webapp which was allowing users to access your data warehouse, you might manage the user/account info using ORM but use JDBC to interact with the data warehouse.
作为附加的注释,这是完全合理的应用程序使用这两个ORM和JDBC。例如,如果您正在构建一个允许用户访问您的数据仓库的 web 应用程序,您可以使用 ORM 管理用户/帐户信息,但使用 JDBC 与数据仓库交互。