Hibernate或JDBC [英] Hibernate or JDBC

查看:119
本文介绍了Hibernate或JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个胖客户端,java swing应用程序,包含25个表格和〜15个JInternalFrames(表格的数据输入表格)。我需要为直接JDBC或ORM(在这种情况下,使用spring框架进行hibernate)进行DBMS交互的设计选择。将来会发生应用程序的构建。



hibernate对于这个大小的项目会过度杀伤吗?对是或否的答案的解释将非常感激(甚至如果有必要,甚至采用不同的方法)。

TIA。

解决方案

好的问题没有一个简单的答案。



在使用Hibernate之后,我曾经是Hibernate的忠实粉丝多年来多个项目。
我曾经相信任何项目都应该默认为hibernate。



今天我不确定。



Hibernate(和JPA)对于某些事情非常有用,特别是在开发周期的早期。
与Hibernate相比,使用Hibernate的速度要快于使用JDBC的速度。
您可以免费获得许多功能 - 缓存,乐观锁定等。

另一方面,它有一些隐藏的成本。 当你开始使用Hibernate时,Hibernate变得非常简单。按照一些教程,在你的课堂上加上一些注释 - 并且你有自己的坚持。但这并不简单,要编写好的代码需要对内部工作和数据库设计有很好的理解。如果您刚刚开始,您可能没有意识到一些可能会在稍后出现的问题,所以这里列出的不完整。



运行时性能足够好,我还没有看到hibernate是 production 中性能不佳的原因。问题在于启动性能以及它如何影响您的单元测试时间和开发性能。当hibernate加载时,它会分析所有实体并进行大量的预缓存 - 对于不是很大的应用程序,它可能需要大约5-10-15秒。所以你的1秒单元测试现在需要11秒。没有趣。



数据库独立性

只要您不需要对数据库做一些微调。



内存中会话



对于每一个事务,Hibernate都会将一个对象存储在内存中,用于它所触及的每个数据库行。当你做一些简单的数据输入时,这是一个很好的优化。如果您因为某些原因需要处理大量对象,除非您自己清楚明确地清理内存中的会话,否则会严重影响性能。



级联



级联允许您简化使用对象图的工作。例如,如果你有一个根对象和一些子对象,并且你保存了根对象,你也可以配置hibernate来保存子对象。当对象图变得复杂时,问题就开始了。除非你非常小心,并且对内部发生的事情有很好的理解,否则很容易搞砸了。当你这样做时,很难调试这些问题。
$ b

延迟加载

延迟加载意味着每次加载对象时,hibernate都不会加载所有相关对象,而是提供占位符,只要您尝试访问它们,它们就会被解析。大优化的权利?它是,除了你需要知道这种行为,否则你会得到神秘的错误。 Google以LazyInitializationException为例。并注意性能。根据您如何加载对象和对象图的顺序,您可能会遇到n + 1选择问题。 Google提供了更多信息。
$ b

架构升级

Hibernate允许简单的架构更改通过重构java代码并重新启动。当你开始时,这很棒。但是,然后你发布第一版。除非你想失去你的客户,否则你需要为他们提供架构升级脚本。这意味着不需要更简单的重构,因为所有模式更改都必须在SQL中完成。

视图和存储过程

Hibernate需要对数据的独占写权限与...合作。这意味着你不能真正使用视图,存储过程和触发器,因为那些可能导致数据发生变化,而休眠不知道它们。您可以让一些外部进程在单独的事务中将数据写入数据库。但是,如果你这样做,你的缓存将有无效的数据。这是另一件值得关注的事情。

单线程会话



Hibernate会话是单线程的。通过会话加载的任何对象只能从同一个线程访问(包括读取)。这对于服务器端应用程序是可以接受的,但是如果您正在执行基于GUI的应用程序,可能会使事情变得复杂。



我想我的观点是没有免费餐。

Hibernate是一个很好的工具,但它是一个复杂的工具,需要时间来正确理解它。如果您或您的团队成员不具备这些知识,那么使用纯JDBC(或Spring JDBC)作为单个应用程序可能会更简单快捷。另一方面,如果你愿意投入时间学习它(包括通过实践学习和调试),那么你将能够更好地理解权衡。

I have a thick client, java swing application with a schema of 25 tables and ~15 JInternalFrames (data entry forms for the tables). I need to make a design choice of straight JDBC or ORM (hibernate with spring framework in this case) for DBMS interaction. Build out of the application will occur in the future.

Would hibernate be overkill for a project of this size? An explanation of either yes or no answer would be much appreciated (or even a different approach if warranted).

TIA.

解决方案

Good question with no single simple answer.

I used to be a big fan of Hibernate after using it in multiple projects over multiple years. I used to believe that any project should default to hibernate.

Today I am not so sure.

Hibernate (and JPA) is great for some things, especially early in the development cycle. It is much faster to get to something working with Hibernate than it is with JDBC. You get a lot of features for free - caching, optimistic locking and so on.

On the other hand it has some hidden costs. Hibernate is deceivingly simple when you start. Follow some tutorial, put some annotations on your class - and you've got yourself persistence. But it's not simple and to be able to write good code in it requires good understanding of both it's internal workings and database design. If you are just starting you may not be aware of some issues that may bite you later on, so here is an incomplete list.

Performance

The runtime performance is good enough, I have yet to see a situation where hibernate was the reason for poor performance in production. The problem is the startup performance and how it affects your unit tests time and development performance. When hibernate loads it analyzes all entities and does a lot of pre-caching - it can take about 5-10-15 seconds for a not very big application. So your 1 second unit test is going to take 11 secods now. Not fun.

Database Independency

It is very cool as long as you don't need to do some fine tuning on the database.

In-memory Session

For every transaction Hibernate will store an object in memory for every database row it "touches". It's a nice optimization when you are doing some simple data entry. If you need to process lots of objects for some reason though, it can seriously affect performance, unless you explicitly and carefully clean up the in-memory session on your own.

Cascades

Cascades allow you to simplify working with object graphs. For example if you have a root object and some children and you save root object, you can configure hibernate to save children as well. The problem starts when your object graph grow complex. Unless you are extremely careful and have a good understanding of what goes on internally, it's easy to mess this up. And when you do it is very hard to debug those problems.

Lazy Loading

Lazy Loading means that every time you load an object, hibernate will not load all it's related objects but instead will provide place holders which will be resolved as soon as you try to access them. Great optimization right? It is, except you need to be aware of this behaviour otherwise you will get cryptic errors. Google "LazyInitializationException" for an example. And be careful with performance. Depending on the order of how you load your objects and your object graph you may hit "n+1 selects problem". Google it for more information.

Schema Upgrades

Hibernate allows easy schema changes by just refactoring java code and restarting. It's great when you start. But then you release version one. And unless you want to lose your customers you need to provide them schema upgrade scripts. Which means no more simple refactoring as all schema changes must be done in SQL.

Views and Stored Procedures

Hibernate requires exclusive write access to the data it works with. Which means you can't really use views, stored procedures and triggers as those can cause changes to data with hibernate not aware of them. You can have some external processes writing data to the database in a separate transactions. But if you do, your cache will have invalid data. Which is one more thing to care about.

Single Threaded Sessions

Hibernate sessions are single threaded. Any object loaded through a session can only be accessed (including reading) from the same thread. This is acceptable for server side applications but might complicate things unnecessary if you are doing GUI based application.

I guess my point is that there are no free meals.

Hibernate is a good tool, but it's a complex tool, and it requires time to understand it properly. If you or your team members don't have such knowledge it might be simpler and faster to go with pure JDBC (or Spring JDBC) for a single application. On the other hand if you are willing to invest time into learning it (including learning by doing and debugging) than in the future you will be able to understand the tradeoffs better.

这篇关于Hibernate或JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆