简体   繁体   English

何时使用Hibernate / JPA / Toplink?

[英]When to use Hibernate/JPA/Toplink?

Right now I'm making an extremely simple website- about 5 pages. 现在我正在制作一个非常简单的网站 - 大约5页。 Question is if it's overkill and worth the time to integrate some sort of database mapping solution or if it would be better to just use plain old JNDI. 问题是,如果它是过度的,值得花时间集成某种数据库映射解决方案,或者如果只使用普通的旧JNDI会更好。 I'll have maybe a dozen things I need to read/write from the database. 我可能需要从数据库中读取/写入十几件东西。 I guess I have a basic understanding of these technologies but it would still take a lot of referring to the documentation. 我想我对这些技术有基本的了解,但仍需要大量参考文档。 Anyone else faced with the decision before? 还有其他人面临过这个决定吗?

EDIT: Sorry, I should've specified JNDI to lookup the DB connection and JDBC to perform the operations. 编辑:对不起,我应该指定JNDI来查找数据库连接和JDBC来执行操作。

Short answer: It depends on the complexity you want to support. 简短回答:这取决于您想要支持的复杂性。

Long answer: 答案很长:

First of all, ORM ( object relational mapping - database mapping as you call it - ) and JNDI ( Java Naming and Directory Interfaces ) are two different things. 首先,ORM(对象关系映射 - 您称之为数据库映射 - )和JNDI(Java命名和目录接口)是两回事。

The first as you already know, is used to map the Database tables to classes and objects. 您已经知道的第一个用于将数据库表映射到类和对象。 The second is to provide a lookup mechanism for resources, they may be DataSources, Ejb, Queues or others. 第二种是为资源提供查找机制,它们可以是DataSources,Ejb,Queues或其他。

Maybe your mean "JDBC". 也许你的意思是“JDBC”。

Now as for your question: If it is that simple may be it wouldn't be necessary to implement an ORM. 至于你的问题:如果这很简单可能没有必要实现ORM。 The number tables would be around 5 - 10 at most, and the operations really simple, I guess. 数字表最多约为5 - 10,我猜这些操作非常简单。

Probably using plain JDBC would be enough. 可能使用普通的JDBC就足够了。

If you use the DAO pattern you may change it later to support the ORM strategy if needed. 如果您使用DAO模式,您可以稍后更改它以支持ORM策略(如果需要)。

Like this: Say you have the Employee table 像这样:假设您有Employee表

You create the Employee.java with all the fields of the DB by hand ( it should not take too long ) and a EmployeeDaO.java with methods like: 您可以手动创建带有DB的所有字段的Employee.java(它不应该花太长时间),使用以下方法创建EmployeeDaO.java:

+findById( id ): Employee
+insert( Employee ) 
+update( Employee )
+delete( Employee ) 
+findAll():List<Employee>

And the implementation is quite straight forward: 实施非常简单:

select * from employee where id = ?
insert into employee ( bla, bla, bla ) values ( ? , ? , ? )
update etc. etc 

When ( and If ) your application becomes too complex you may change the DAO implementation . 当(和If)您的应用程序变得过于复杂时,您可能会更改DAO实现。 For instance in the "select" method you change the code to use the ORM object that performs the operation. 例如,在“select”方法中,您可以更改代码以使用执行操作的ORM对象。

public Employee selectById( int id ) {
      // Commenting out the previous implementation...
      // String query = select * from employee where id = ? 
      // execute( query )  

      // Using the ORM solution

       Session session = getSession();
       Employee e = ( Employee ) session.get( Employee.clas, id );
       return e;
}

This is just an example, in real life you may let the abstact factory create the ORM DAO, but that is offtopic. 这只是一个例子,在现实生活中你可以让抽象工厂创建ORM DAO,但这是offtopic。 The point is you may start simple and by using the desing patterns you may change the implementation later if needed. 重点是您可以从简单开始,并且通过使用设计模式,您可以在以后根据需要更改实施。

Of course if you want to learn the technology you may start rigth away with even 1 table. 当然,如果你想学习这项技术,你甚至可以用一张桌子来开始学习。

The choice of one or another ( ORM solution that is ) depend basically on the technology you're using. 选择一种或另一种(ORM解决方案)主要取决于您使用的技术。 For instance for JBoss or other opensource products Hibernate is great. 例如,对于JBoss或其他开源产品,Hibernate非常棒。 It is opensource, there's a lot of resources where to learn from. 它是开源的,有很多资源可供学习。 But if you're using something that already has Toplink ( like the oracle application server ) or if the base is already built on Toplink you should stay with that framework. 但是如果你正在使用已经有Toplink的东西(比如oracle应用服务器),或者如果基础已经建立在Toplink上,你应该继续使用该框架。

By the way, since Oracle bought BEA, they said they're replacing Kodo ( weblogic peresistence framework ) with toplink in the now called "Oracle Weblogic Application Server". 顺便说一句,自甲骨文收购BEA以来,他们表示他们正在用现在所谓的“Oracle Weblogic Application Server”中的toplink取代Kodo(weblogic peresistence framework)。

I leave you some resources where you can get more info about this: 我给你留下了一些资源,你可以从中获得更多信息:


In this "Patterns of Enterprise Application Architecture" book, Martin Fowler, explains where to use one or another, here is the catalog. 在这本“企业应用程序架构模式”一书中,Martin Fowler解释了在哪里使用,这是目录。 Take a look at Data Source Architectural Patterns vs. Object-Relational Behavioral Patterns: 看看数据源架构模式与对象关系行为模式:

PEAA Catalog PEAA目录


DAO ( Data Access Object ) is part of the core J2EE patterns catalog: DAO(数据访问对象)是核心J2EE模式目录的一部分:

The DAO pattern DAO模式


This is a starter tutorial for Hibernate: 这是Hibernate的入门教程:

Hibernate 过冬


The official page of Toplink: Toplink的官方页面:

Toplink 排名靠前


Finally I "think" the good think of JPA is that you may change providers lately. 最后,我“认为”JPA的好想法是你最近可能会改变提供者。

Start simple and then evolve. 从简单开始然后进化。

I hope this helps. 我希望这有帮助。

It does seem like it would be overkill for a very simple application, especially if you don't have plans to expand on it ever. 对于一个非常简单的应用程序来说,它似乎有点过分,特别是如果你没有计划扩展它。 However, it also seems like it could be worthwhile to use those with this simple application so that you have a better understanding of how they work for next time you have something that could use them. 但是,使用这个简单的应用程序似乎也值得一试,以便您可以更好地了解它们在下次有可能使用它们时的工作方式。

Do you mean plain old JDBC? 你的意思是普通的旧JDBC吗? A small project might be a good opportunity to pick up one of the ORM frameworks, especially if you have the time. 一个小项目可能是一个很好的机会来获取一个ORM框架,特别是如果你有时间的话。

Without more information it's hard to provide a recommendation one way or another however. 如果没有更多信息,很难以这种或那种方式提供推荐。

My rule of thumb is if it's read-only, I'm willing to do it in JDBC, although I prefer to use an empty Hibernate project with SQLQuery to take advantage of Hibernate's type mapping. 我的经验法则是,如果它是只读的,我愿意在JDBC中执行它,尽管我更喜欢使用带有SQLQuery的空Hibernate项目来利用Hibernate的类型映射。 Once I have to do writes, I go with Hibernate because it's so much easier to set a few attributes and then call save than to set each column individually. 一旦我必须写入,我就会使用Hibernate,因为设置一些属性然后调用save比单独设置每个列要容易得多。 And when you have to start optimizing to avoid updates on unchanged objects, you're way better off with an OR/M and its dirty checking. 当你必须开始优化以避免更改未更改的对象时,最好使用OR / M及其脏检查。 Dealing with foreign key relationships is another sign that you need to map it once and then use the getters. 处理外键关系是另一个标志,您需要映射一次然后使用getter。 The same logic would apply to Toplink, although unless they've added something like HQL in the 3 years since I used it, Hibernate would be much better for this kind of transition from pure SQL. 相同的逻辑适用于Toplink,虽然除非他们在我使用它的3年内添加了类似HQL的东西,否则Hibernate对于从纯SQL的这种转换会好得多。 Keep in mind that you don't have to map every object/table, just the ones where there's a clear advantage. 请记住,您不必映射每个对象/表,只有那些有明显优势的对象/表。 In my experience, most projects that don't use an existing OR/M end up building a new one, which is a bad idea. 根据我的经验,大多数不使用现有OR / M的项目最终会构建一个新的OR / M,这是一个坏主意。

The best way to learn ORM is on a small project. 学习ORM的最佳方法是在一个小项目上。 Start on this project. 从这个项目开始。

Once you get the hang of it, you'll use ORM for everything. 一旦掌握了它,你将使用ORM来做所有事情。

There's nothing too small for ORM. 对于ORM来说,没有什么比这更小的了。 After your first couple of projects, you'll find that you can't work any other way. 在你的第一个项目之后,你会发现你不能以任何其他方式工作。 The ORM mapping generally makes more sense than almost any other way of working. ORM映射通常比几乎任何其他工作方式更有意义。

Look at the various toplink guides here, they have intro, examples, scenarios etc 看看这里的各种toplink指南,它们有介绍,示例,场景等

http://docs.oracle.com/cd/E14571_01/web.1111/b32441/toc.htm http://docs.oracle.com/cd/E14571_01/web.1111/b32441/toc.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM