Java 休眠与 iBATIS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1984548/
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
Hibernate Vs iBATIS
提问by
For our new product re-engineering, we are in the process of selecting the best framework from Java. As the consideration is to go for database agnostic approach for model, we are working on options between Struts + Spring with iBATIS or Hibernate. Please advice which is best as both offer persistence.
对于我们的新产品重新设计,我们正在从 Java 中选择最佳框架。由于考虑采用与数据库无关的模型方法,我们正在研究 Struts + Spring 与 iBATIS 或 Hibernate 之间的选项。请建议哪个最好,因为两者都提供持久性。
采纳答案by cletus
Ibatis and Hibernate are quite different beasts.
Ibatis 和 Hibernate 是完全不同的野兽。
The way I tend to look at it is this: Hibernate works better if your view is more object-centric. If however you view is more database-centricthen Ibatis is a much stronger choice.
我倾向于这样看待它:如果您的视图更加以对象为中心,Hibernate 会更好地工作。但是,如果您认为更以数据库为中心,那么 Ibatis 是一个更强大的选择。
If you're in complete control of your schema and you don't have an extremely high throughput requirement then Hibernate can work quite well. The object model makes for fairly convenient code but at a hugecomplexity cost.
如果您可以完全控制您的模式并且您没有极高的吞吐量要求,那么 Hibernate 可以很好地工作。对象模型使得用于还算方便代码,但在巨大的复杂性成本。
If you're dealing with a "legacy" database schema where you need to write fairly complicated SQL queries then chances are Ibatis will work better.
如果您正在处理需要编写相当复杂的 SQL 查询的“遗留”数据库模式,那么 Ibatis 可能会更好地工作。
HQL (Hibernate Query Language) is another language you'll have to learn and even then you'll probably find cases where you stillneed to write SQL. What's more, chances are you will at some spend half a day figuring out the right combination of XML, properties, annotations, etc to get Hibernate to generate a performant SQL query.
HQL(Hibernate Query Language)是另一种你必须学习的语言,即便如此,你也可能会发现仍然需要编写 SQL 的情况。更重要的是,您可能会花费半天时间找出 XML、属性、注释等的正确组合,以使 Hibernate 生成高性能 SQL 查询。
There is no universal "A is better than B" answer for this question.
对于这个问题,没有普遍的“A 比 B 好”的答案。
回答by Pascal Thivent
Cletus did a great job at summarizing this comparison. Hibernate works well when you control the data model and is more object-centric while iBATIS works well when you need to integrate with an existing database and is more data-centric.
Cletus 在总结这种比较方面做得很好。当您控制数据模型并且更加以对象为中心时,Hibernate 运行良好,而当您需要与现有数据库集成并且更加以数据为中心时,iBATIS 运行良好。
Also I think that Hibernate has a bit more of learning curve. With iBATIS, it's pretty easy to know what is going on while more "magic" happens with Hibernate. In other words, newbies might find iBatis easier to use and to understand.
另外我认为 Hibernate 有更多的学习曲线。使用 iBATIS,很容易知道发生了什么,而更多的“魔法”发生在 Hibernate。换句话说,新手可能会发现 iBatis 更易于使用和理解。
But I'm not saying that you should prefer iBatis, iBatis and Hibernate are just different as said above.
但我并不是说你应该更喜欢 iBatis,iBatis 和 Hibernate 只是如上所述不同。
And by the way, if you go for Hibernate, maybe consider using standardized JPA and EJB 3.0 (JSR-220) object/relational mapping annotations provided by Hibernate Annotations.
顺便说一句,如果您选择 Hibernate,可以考虑使用Hibernate Annotations提供的标准化 JPA 和 EJB 3.0 (JSR-220) 对象/关系映射注释。
回答by duffymo
if you're already using Spring, I would start with Spring JDBC rather than plunging right into Hibernate or iBatis. If you write your persistence tier in terms of interfaces, you should have no problem switching implementations after you've gotten Hibernate or iBatis under your belt.
如果您已经在使用 Spring,我会从 Spring JDBC 开始,而不是直接使用 Hibernate 或 iBatis。如果您根据接口编写持久层,那么在您掌握了 Hibernate 或 iBatis 之后,切换实现应该没有问题。
There's no reason why it has to be an "all or none" decision. Use what's best for your situation.
没有理由必须做出“全有或全无”的决定。使用最适合您情况的方法。
回答by Joseph Lust
Consider what you're trying to achieve. Typically, the Command Query Response Segregationmodel works well for complex domains.
考虑您要实现的目标。通常,命令查询响应隔离模型适用于复杂域。
The reason is that you're trying to do one of two things typically:
原因是您通常尝试执行以下两种操作之一:
- Create/Update/Delete some complex domain entities
- Run analytic fetch queries (i.e. summation/aggregation queries)
- 创建/更新/删除一些复杂的域实体
- 运行分析获取查询(即求和/聚合查询)
Hibernateworks well for case 1 allowing you to just make a POJO and persist/update it. It also does this quickly, unless your domain is quite large.
Hibernate适用于案例 1,允许您只制作一个 POJO 并保留/更新它。它也可以快速完成此操作,除非您的域非常大。
myBatisis great for fetch queries (case 2) where you just want an answer. Hibernate would attempt to load the entire object graph and you'd need to start tuning queries with LazyLoading tricks to keep it working on a large domain. Conversely if you just want some analytic POJO page, the myBatis implementation of the same query would be trivial.
myBatis非常适合您只需要一个答案的 fetch 查询(案例 2)。Hibernate 会尝试加载整个对象图,您需要使用 LazyLoading 技巧开始调整查询以使其在大型域上工作。相反,如果您只想要一些分析 POJO 页面,则相同查询的 myBatis 实现将是微不足道的。
Because of this, myBatis is faster than Hibernateat SELECTS.
正因为如此,myBatis在 SELECTS 上比 Hibernate 更快。
These two cases are the difference between Commandswhere you want to change the domain data and Responseswhere you just want to fetch some data.
这两种情况的区别命令要更改域数据和反应,你只是想获取一些数据。
So, consider these two cases and what your application does. If you have a simple domain and just fetch information, use myBatis. If you have a complex domain and persist entities, use Hibernate. If you do both, consider a hybrid approach. That's what we use on our project that has thousands of entities to keep it under control. ;)
因此,请考虑这两种情况以及您的应用程序的作用。如果您有一个简单的域并且只是获取信息,请使用 myBatis。如果您有一个复杂的域和持久化实体,请使用 Hibernate。如果两者都做,请考虑混合方法。这就是我们在我们的项目中使用的内容,该项目有数千个实体来控制它。;)
回答by johnm
Hibernate is an ORM, meaning (at its most basic level) it maps instances of java objects to actual rows in a database table. Generally, for pojo's retrieved via Hibernate: any manipulations and modifications to these pojo's will appear in the database. Hibernate will generate and execute the relevant SQL at an appropriate time.
Hibernate 是一个 ORM,这意味着(在最基本的层面上)它将 java 对象的实例映射到数据库表中的实际行。通常,对于通过 Hibernate 检索的 pojo:对这些 pojo 的任何操作和修改都将出现在数据库中。Hibernate 会在适当的时候生成并执行相关的 SQL。
Mybatis (at its most basic level) is simply a tool for piecing together and executing SQL that is stored in xml files. It does not map instances of Java objects to rows in a database table, rather it maps Java methods to SQL statements, and therefore it is not an ORM. It can also return pojo's of course, but they are not tied to any kind of a persistence context.
Mybatis(在其最基本的层面上)只是一个用于拼凑和执行存储在 xml 文件中的 SQL 的工具。它不会将 Java 对象的实例映射到数据库表中的行,而是将 Java 方法映射到 SQL 语句,因此它不是 ORM。当然,它也可以返回 pojo,但它们与任何类型的持久性上下文无关。
Both tools do a lot more than described above, but one is an ORM and one is not.
这两种工具的功能都比上面描述的要多得多,但一个是 ORM,一个不是。
The criteria to enable you to choose which one to use, I believe, depends critically on the database model you have to work with.
我相信,使您能够选择使用哪一个的标准在很大程度上取决于您必须使用的数据库模型。
For example imagine a large sprawling schema, representing some insurance model. Developers are required to retrieve data, and interact with that data in a way that meets the business at hand.
例如,想象一个庞大的架构,代表某个保险模型。开发人员需要检索数据,并以符合手头业务的方式与该数据进行交互。
Developer's come on go, and would never be expected to have the requisite business knowledge to write allthe sql by hand (which Mybatis would require). Hibernate would suit a scenario like that.
开发人员来吧,永远不会期望拥有 手动编写所有sql所需的业务知识(Mybatis 需要)。Hibernate 适合这样的场景。
Business analysts define the datamodel, the entities, the relationships and the interactions, as is their expertise. Java developer's then use Hibernate to "walk the model". The business developer's can become very productive quickly without the need to write complicated error prone sql to run on a very complicated schema.
业务分析师定义数据模型、实体、关系和交互,以及他们的专业知识。Java 开发人员然后使用 Hibernate 来“走模型”。业务开发人员可以很快变得非常高效,而无需编写复杂的、容易出错的 sql 来在非常复杂的模式上运行。
In my expierence, both Hibernate and Mybatis are used regularly on the same project.
在我的经验中,Hibernate 和 Mybatis 都经常用于同一个项目。
Where Hibernate is being used for
Hibernate 用于的地方
- General C.R.U.D functionality
- 'Walking' the 'domain object' relational model
- Session management
- 常规 CRUD 功能
- '走''领域对象'关系模型
- 会话管理
and where Mybatis is being used for
以及 Mybatis 的用途
- ad hoc queries
- kick off (and interact with) stored procedures
- support very specific or intricate queries
- support complicated search queries, where search criteria is dynamic, and paging of results
- 即席查询
- 启动(并与之交互)存储过程
- 支持非常具体或复杂的查询
- 支持复杂的搜索查询,其中搜索条件是动态的,以及结果分页
回答by Justas
ORM vs persistence framework
ORM vs 持久化框架
Hibernate is object-relation mapping framework (ORM) which maps Java classes to database tables. MyBatis is persistence framework - not ORM. It maps SQL statements to Java methods.
Hibernate 是对象关系映射框架 (ORM),它将 Java 类映射到数据库表。MyBatis 是持久性框架 - 不是 ORM。它将 SQL 语句映射到 Java 方法。
Database schema
数据库模式
Hibernate can create or validate database schema according to your Java model while MyBatis does not have such feature. Also it is convenient for testing environment when you're using in-memory DB. Related discussions:
Hibernate 可以根据您的 Java 模型创建或验证数据库模式,而 MyBatis 没有这样的功能。当您使用内存数据库时,它也方便测试环境。相关讨论:
Cache
缓存
Hibernate has first level cache which is impossible to disable. It means that if you query item through ORM and then delete it directly with SQL, it stays in the cache. You can explicitly clear the cache to get the most updated results from database. Related discussions:
Hibernate 具有无法禁用的一级缓存。这意味着如果你通过ORM查询item,然后直接用SQL删除它,它会留在缓存中。您可以明确清除缓存以从数据库中获取最新的结果。相关讨论:
- Do Jpa& Hibernate load data which changes asynchronously in DB?
- What are First and Second Level caching in Hibernate?
Optimistic lock management
乐观锁管理
Also there are differences for optimistic lock management:
乐观锁管理也存在差异:
MyBatis doesn't support optimistic concurrency control natively, unlike ORM tools like Hibernate/JPA with the @Version annotation.
MyBatis 本身并不支持乐观并发控制,这与 Hibernate/JPA 等带有 @Version 注释的 ORM 工具不同。
Related discussions:
相关讨论:
Lazy loading
延迟加载
Hibernate will try to load entire object graph except objects which are marked for lazy loading. myBatis will load data according a SQL query. Lazy loading may improve performance but it may cause connection leaks if it used with
<property name="hibernate.enable_lazy_load_no_trans" value="true" />
properties. Related discussions:
Hibernate 将尝试加载整个对象图,除了标记为延迟加载的对象。myBatis 将根据 SQL 查询加载数据。延迟加载可能会提高性能,但如果与<property name="hibernate.enable_lazy_load_no_trans" value="true" />
属性一起使用,可能会导致连接泄漏
。相关讨论:
- org.hibernate.LazyInitializationException - could not initialize proxy - no Session
- Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
- org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话
- 使用 hibernate.enable_lazy_load_no_trans 解决 Hibernate Lazy-Init 问题
Hibernate Session management
休眠会话管理
Entities operations like saving, updating or deleting are performed via Hibernate Session. It requires good understanding how to implement proper Hibernate Session management strategy to avoid detached entity passed to persist
and other phenomenons related to Hibernate.
实体的保存、更新或删除等操作都是通过 Hibernate Session执行的。它需要很好地理解如何实施适当的 Hibernate 会话管理策略来避免detached entity passed to persist
与 Hibernate 相关的其他现象。
Sometimes it may take more time trying to understand underlying Hibernate behavior than add a little bit more work and write raw SQL statements for myBatis.
有时,与为 myBatis 添加更多工作和编写原始 SQL 语句相比,尝试理解底层 Hibernate 行为可能需要更多时间。
Cascading
级联
Hibernate provides cascading, orphan removal and other features for object graphs while they not present in myBatis - to implement them you'll need to write SQL queries explicitly.
Hibernate 为对象图提供了级联、孤儿移除和其他功能,而它们在 myBatis 中不存在 - 要实现它们,您需要明确地编写 SQL 查询。
Queries
查询
In myBatis you'll write almost plain SQL queries. Hibernate has multiple options to form query: SQL, HQL, Criteria API. Sometimes it may be suitable to use Criteria API when you have many optional fields in criteria. It would provide more structured approach to form query and maybe avoid related mistakes.
在 myBatis 中,您将编写几乎简单的 SQL 查询。Hibernate 有多个选项来形成查询:SQL、HQL、Criteria API。有时,当您的标准中有许多可选字段时,可能适合使用 Criteria API。它将提供更结构化的表单查询方法,并可能避免相关错误。