Java JDBC VS 休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28163670/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:42:43  来源:igfitidea点击:

JDBC VS Hibernate

javamysqlhibernatejpajdbc

提问by Lemon Juice

We have been using JDBC for a very long time in our web applications. The main reason we used it is because we have 100% control over the code, sql and fix things by our hands. Apart from that we used triggers inside the database, and the database is developed separately by DB experts.

我们在 Web 应用程序中使用 JDBC 已经很长时间了。我们使用它的主要原因是因为我们可以 100% 控制代码、sql 并通过我们的手修复问题。除此之外我们在数据库内部使用了触发器,数据库由DB专家单独开发。

However many now recommend using Hibernateso we also thought about using it. But, we found the below issues.

但是现在很多人推荐使用,Hibernate所以我们也考虑使用它。但是,我们发现了以下问题。

  1. Hibernate cannot connect with an "Existing" database. It always try to create a one of its own.

  2. Our database might access by same application which is in different platforms (cloud, server, VPS, Personal Computer). Hibernate can make problems because of its caching in this situation.

  3. We never like to give the "table creating work" to the java code. We create tables manually, always.

  4. We might have to use very long and complex SQL statements. Last time we used an statement with more than 150 lines, joining more than 20 tables. We doubt whether we will face troubles in this when it comes to Hibernate.

  5. Our SQL code is nice and standard. Hibernate generated code seems to be bit dirty for us.

  6. We always use MySQL. Never use any other DB.

  7. The application we create require max security, related to medical. If at least one data record is leaked, we are done.

  8. There are lot of foreign keys, Primary Keys, Composite Keys, Unique Keysetc etc in database. In forums, some complained that Hibernate messed with those.

  9. We decided to try hibernate because some people claims, "Are you Software Engineers? You are using already dead JDBC!!. "

  1. Hibernate 无法与“现有”数据库连接。它总是试图创造一个自己的。

  2. 我们的数据库可能会被不同平台(云、服务器、VPS、个人计算机)中的相同应用程序访问。在这种情况下,Hibernate 可能会因为它的缓存而产生问题。

  3. 我们从不喜欢将“表创建工作”交给 java 代码。我们总是手动创建表。

  4. 我们可能不得不使用非常长且复杂的 SQL 语句。上次我们用了150多行的语句,连接了20多个表。我们怀疑当涉及到 Hibernate 时,我们是否会在这方面遇到麻烦。

  5. 我们的 SQL 代码很好而且很标准。Hibernate 生成的代码对我们来说似乎有点脏。

  6. 我们总是使用 MySQL。永远不要使用任何其他数据库。

  7. 我们创建的应用程序需要最大的安全性,与医疗相关。如果至少有一个数据记录被泄露,我们就完成了。

  8. 有很多的foreign keysPrimary KeysComposite KeysUnique Keys等等等等,在数据库中。在论坛上,有些人抱怨 Hibernate 搞砸了这些。

  9. 我们决定尝试 hibernate 是因为有些人声称,“你们是软件工程师吗?你们使用的已经死了JDBC!!”

Considering these, please let me know whether the above points are actually true (as I said, I got to know them via googling, discussion etc) or not. And, what are the pros and cons of Hibernate VS Java JDBC?

考虑到这些,请让我知道以上几点是否真的正确(正如我所说,我是通过谷歌搜索、讨论等了解它们的)。并且,Hibernate VS Java JDBC 的优缺点是什么?

回答by Andremoniy

Answering issues listed above:

回答上面列出的问题:

1. Hibernate cannot connect with an "Existing" database. It always try to create a one of its own.

1. Hibernate 无法连接“现有”数据库。它总是试图创造一个自己的。

This is wrong. Hibernate canconnect to an existing database, and it doesn't always try to recreate it. You just should turn of parameter like hbm2ddl. auto.

这是错误的。Hibernate可以连接到现有的数据库,并且它并不总是尝试重新创建它。你应该把像hbm2ddl. auto.

2. Our database might access by same application which is in different platforms (cloud, server, VPS, Personal Computer). Hibernate can make problems because of its caching in this situation.

2. 我们的数据库可能会被不同平台(云、服务器、VPS、个人电脑)中的同一个应用程序访问。在这种情况下,Hibernate 可能会因为它的缓存而产生问题。

Hibernate has an adjustable cache, so this is also not a problem.

Hibernate 有一个可调整的缓存,所以这也不是问题。

3. We never like to give the "table creating work" to the java code. We create tables manually, always.

3.我们从不喜欢把“建表工作”交给java代码。我们总是手动创建表。

No problem. See p.1 above. Furthemore there are several convinient libraries for indirect table creation and update (e.g. liquibase) which can be used in couple with hibernate perfectly.

没问题。请参阅上面的第 1 页。此外,还有几个方便的用于间接表创建和更新的库(例如liquibase),它们可以与 hibernate 完美结合使用。

4. We might have to use very long and complex SQL statements. Last time we used an statement with more than 150 lines, joining more than 20 tables. We doubt whether we will face troubles in this when it comes to Hibernate.

4. 我们可能不得不使用很长很复杂的 SQL 语句。上次我们用了150多行的语句,连接了20多个表。我们怀疑当涉及到 Hibernate 时,我们是否会在这方面遇到麻烦。

You can always use direct JDBC calls and invoke native SQL queries via hibernate, if it is neeeded.

如果需要,您始终可以使用直接 JDBC 调用并通过休眠调用本机 SQL 查询。

5. Our SQL code is nice and standard. Hibernate generated code seems to be bit dirty for us.

5. 我们的 SQL 代码很好,很标准。Hibernate 生成的代码对我们来说似乎有点脏。

Again, if you have to invoke some logic complicated SQL code instead of hibernate auto-generated - you can do it.

同样,如果您必须调用一些逻辑复杂的 SQL 代码而不是自动生成的休眠状态 - 您可以这样做。

6. We always use MySQL. Never use any other DB.

6. 我们一直使用 MySQL。永远不要使用任何其他数据库。

Not a problem at all. Hibernate has special MySQL dialect support: org.hibernate.dialect.MySQLDialect.

根本不是问题。Hibernate 有特殊的 MySQL 方言支持:org.hibernate.dialect.MySQLDialect.

7. The application we create require max security, related to medical. If at least one data record is leaked, we are done.

7. 我们创建的应用程序需要最大的安全性,与医疗相关。如果至少有一个数据记录被泄露,我们就完成了。

Security issues aren't related to ORM techniques. Hibernateis just logical and convinient object-oriented layer between pure database JDBC calls and programmers tools. It doesn't influence somehow on common net security.

安全问题与 ORM 技术无关。Hibernate只是纯数据库 JDBC 调用和程序员工具之间的逻辑和方便的面向对象层。它不会以某种方式影响公共网络安全。

回答by Junaid

Here are the quick answers that I know

这是我知道的快速答案

1) You can connect to an existing database. But yeah as stated here

1) 您可以连接到现有数据库。但是,正如此处所述

If you don't have a solid object model, I'd say that Hibernate is a terrible choice.

如果您没有实体对象模型,我会说 Hibernate 是一个糟糕的选择。

2) As you database is been accessed from different applications so you can maintain locks. On-the-other-hand you can trun-offcaching as done here.

2)当您从不同的应用程序访问数据库时,您可以维护锁。另一方面,您可以像此处那样关闭缓存。

3) You can create tables manually and connect it using .hbm.xmlfile.

3)您可以手动创建表并使用.hbm.xml文件连接它。

4) You can use any type of query in hibernate like simple SQL queries criteria.

4) 您可以在休眠中使用任何类型的查询,例如简单的 SQL 查询条件。

5) You can directly use SQL code in Hibernate, if you want. Other option is to use criteria.

5) 如果需要,您可以直接在 Hibernate 中使用 SQL 代码。另一种选择是使用标准。

6) Hibernate is NOT DB specific. You can go for any Database and connect it with hibernate.

6) Hibernate 不是特定于数据库的。您可以使用任何数据库并将其与休眠连接。

7) Using locks and giving rights in database you can maintain security.

7) 在数据库中使用锁和授予权限可以维护安全性。

8) Agreed that foreign keys are messy in Hibernate If You Donot Handle It Well. So Use OO approach and maintain cascades well, then Hibernate will be good choice.

8) 同意如果你处理不当,外键在 Hibernate 中会很混乱。所以使用 OO 方法并保持好级联,那么 Hibernate 将是不错的选择。

回答by Ankur Singhal

Using plain old JDBC, does not mean you are lacking in IT industry, rather Hibernate also uses JDBC in the underlying layer.

使用普通的老式JDBC,并不代表你在IT行业就缺乏,而是Hibernate在底层也使用了JDBC。

What advantages it gives us what we should look for.

它给了我们什么好处,我们应该寻找什么。

1.) CacheMechanism.

1.)Cache机制。

2.) Managing sessions, transactionsetc.

2.)管理sessionstransactions等等。

3.) Reduce efforts in writing queries, more utilities of hibernate like Query API, Criteria API, HQL

3.) 减少编写查询的工作量,更多的 hibernate 实用程序,如Query API, Criteria API,HQL

The questions that you have raised are more or less covered in Hibernate docs.

您提出的问题或多或少都包含在Hibernate 文档中

Also there are lot more caching strategy available ehcache, infinispan, depends on the server we are deploying, JBOSS, Weblogic, Tomcat etc. ++ environment like cloud, distributed cache etc.

还有更多可用的缓存策略ehcache,infinispan,取决于我们正在部署的服务器,JBOSS,Weblogic,Tomcat等++环境,如云,分布式缓存等。

Hibernate still provides you with option of turning off automatically creating schema and pointing to the one create by you.

Hibernate 仍然为您提供关闭自动创建模式并指向您创建的模式的选项。

回答by Vlad Mihalcea

Hibernate is a great tool and you'll find plenty of documentations, booksand blog articlesabout it.

Hibernate 是一个很棒的工具,您会找到大量关于它的文档书籍博客文章

I will address all your concerns:

我将解决您的所有疑虑:

Hibernate cannot connect with an "Existing" database. It always try to create a one of its own.

Hibernate 无法与“现有”数据库连接。它总是试图创造一个自己的。

Hibernate should use a separate database schema management procedure even for integration testing. You should use an incremental versioning tool like FlywayDBto manage your schema changes.

即使对于集成测试,Hibernate 也应该使用单独的数据库模式管理过程。您应该使用像FlywayDB这样的增量版本控制工具来管理您的架构更改。

Our database might access by same application which is in different platforms (cloud, server, VPS, Personal Computer). Hibernate can make problems because of its caching in this situation.

我们的数据库可能会被不同平台(云、服务器、VPS、个人计算机)中的相同应用程序访问。在这种情况下,Hibernate 可能会因为它的缓存而产生问题。

You don't have to use the 2nd level cache, which uses 3rd party caching implementations. All caching solutions may break transactional consistency. The first level cache guarantees session-level repeatablereads and with the optimistic locking in place you can prevent lost updates.

您不必使用 2nd 级缓存,它使用 3rd 方缓存实现。所有缓存解决方案都可能破坏事务一致性。第一级缓存保证了会话级的可重复读取,并且使用乐观锁定可以防止丢失更新

We never like to give the "table creating work" to the java code. We create tables manually, always.

我们从不喜欢将“表创建工作”交给 java 代码。我们总是手动创建表。

The database management should be separated from your ORM tool. That's a best practice anyway.

数据库管理应该与您的 ORM 工具分开。无论如何,这是最佳做法。

We might have to use very long and complex SQL statements. Last time we used an statement with more than 150 lines, joining more than 20 tables. We doubt whether we will face troubles in this when it comes to Hibernate.

我们可能不得不使用非常长且复杂的 SQL 语句。上次我们用了150多行的语句,连接了20多个表。我们怀疑当涉及到 Hibernate 时,我们是否会在这方面遇到麻烦。

Hibernate is great for write operations and for concurrency control. You still need to use native SQL for advanced queries (window functions, CTE). But Hibernate allows you to run native queries.

Hibernate 非常适合写操作和并发控制。您仍然需要使用本机 SQL 进行高级查询(窗口函数、CTE)。但是 Hibernate 允许您运行本机查询。

Our SQL code is nice and standard. Hibernate generated code seems to be bit dirty for us.

我们的 SQL 代码很好而且很标准。Hibernate 生成的代码对我们来说似乎有点脏。

You don't need and you shouldn't probably use the hbmdll utility anyway.

您不需要也不应该使用 hbmdll 实用程序。

We always use MySQL. Never use any other DB.

我们总是使用 MySQL。永远不要使用任何其他数据库。

That's even better. You can therefore use advance native queries without caring for database portability issues.

那更好。因此,您可以使用高级本机查询而无需关心数据库可移植性问题。

The application we create require max security, related to medical. If at least one data record is leaked, we are done.

我们创建的应用程序需要最大的安全性,与医疗相关。如果至少有一个数据记录被泄露,我们就完成了。

Hibernate doesn't prevent you from securing your database or the data access code. You can still use database security measures with Hibernate too. You can even use Jasyptto enable all sorts of security-related features:

Hibernate 不会阻止您保护数据库或数据访问代码。您仍然可以在 Hibernate 中使用数据库安全措施。您甚至可以使用Jasypt启用各种与安全相关的功能:

  • advanced password hashing
  • two-way encryption
  • 高级密码散列
  • 双向加密

There are lot of foreign keys, Primary Keys, Composite Keys, Unique Keys etc etc in database. In forums, some complained that Hibernate messed with those.

数据库中有很多外键、主键、复合键、唯一键等。在论坛上,有些人抱怨 Hibernate 搞砸了这些。

All of those are supported by Hibernate. Aside from the JPA conventions, Hibernate also offers particular mappingfor any exotic mapping.

Hibernate 支持所有这些。除了 JPA 约定之外,Hibernate 还为任何外来映射提供特定的映射。

We decided to try hibernate because some people claims, "Are you Software Engineers? You are using already dead JDBC !!. "

我们决定尝试 hibernate 是因为有些人声称,“你们是软件工程师吗?你们在使用已经死了的 JDBC !!。”

That's not the right argument for switching from a library you already master. If you think you can benefit from using Hibernate then that's the only compelling reason for switching from JDBC.

这不是从您已经掌握的库切换的正确论据。如果您认为您可以从使用 Hibernate 中受益,那么这是从 JDBC 切换的唯一令人信服的理由。