何时使用Hibernate / JPA / Toplink? [英] When to use Hibernate/JPA/Toplink?

查看:115
本文介绍了何时使用Hibernate / JPA / Toplink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在制作一个非常简单的网站 - 大约5页。问题在于它是否过度杀伤,值得花时间整合某种数据库映射解决方案,或者如果使用普通的旧式JNDI会更好。我可能会需要从数据库读取/写入数十个东西。我想我对这些技术有一个基本的了解,但它仍然需要大量的参考文档。任何其他人面临的决定之前?

编辑:对不起,我应该已经指定JNDI来查找数据库连接和JDBC执行操作。

解决方案

简短回答:这取决于您想要支持的复杂性。 :首先,ORM(对象关系映射 - 数据库映射就像你所说的那样)和JNDI(Java命名和目录接口)是两个不同的东西。

第一个是你已经知道的,用于将数据库表映射到类和对象。第二个是为资源提供查找机制,它们可能是DataSources,Ejb,Queues或其他。



也许你的意思是JDBC。



至于你的问题:如果是这么简单的话,可能不需要实现一个ORM。我想,这个数字表格最多只有5到10个,而且操作非常简单。



可能使用普通JDBC就足够了。



如果您使用DAO模式,您可以稍后将其更改为如果需要支持ORM策略。



像这样:
假设您拥有Employee表



您创建Employee.java所有的数据库字段(不应该花太长时间)和一个EmployeeDaO.java类似的方法:

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

执行过程非常简单:

  select * from employee where id =? 
插入员工(bla,bla,bla)值(?,?,?)
更新等等等等

当(和If)你的应用程序变得太复杂时,你可能会改变DAO的实现。例如,在select方法中,您将代码更改为使用执行该操作的ORM对象。

  public Employee selectById int id){
//注释掉以前的实现...
// String query = select * from employee where id =?
//执行(查询)

//使用ORM解决方案

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





$ b

这只是一个例子,在现实生活中你可以让抽象工厂创建ORM DAO,但这是不合时宜的。关键是你可以开始简单,通过使用设计模式,如果需要,你可以稍后改变实现。



当然,如果你想学习这项技术,你甚至可以开始使用一张表。选择一种或另一种(ORM解决方案)基本上取决于您使用的技术。例如对于JBoss或其他开源产品来说,Hibernate非常棒。它是开源的,有很多资源可以从中学习。但是如果你使用的东西已经有了Toplink(比如oracle应用服务器),或者如果基础已经建立在Toplink上,你应该使用该框架。顺便说一句,由于甲骨文收购了BEA,他们表示他们正在用名为Oracle Weblogic Application Server的顶级链接取代Kodo(weblogic peresistence framework)。

/ p>

我给你留下一些资源,你可以从这里获得更多信息:



在这本企业应用架构模式一书中,Martin Fowler解释了在哪里使用这个或那个,这里是目录。看看数据源架构模式与对象关系行为模式:


$ b DAO(数据访问对象)是核心J2EE模式目录的一部分: / p>

DAO模式

a>






这是Hibernate的入门教程:

Hibernate






Toplink的官方网页:

Toplink






最后,我认为最近你可能会改变提供者。

从简单开始,然后发展。



我希望这有助于。


Right now I'm making an extremely simple website- about 5 pages. 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. 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.

解决方案

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.

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.

Maybe your mean "JDBC".

Now as for your question: If it is that simple may be it wouldn't be necessary to implement an ORM. The number tables would be around 5 - 10 at most, and the operations really simple, I guess.

Probably using plain JDBC would be enough.

If you use the DAO pattern you may change it later to support the ORM strategy if needed.

Like this: Say you have the Employee table

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:

+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 . For instance in the "select" method you change the code to use the ORM object that performs the operation.

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. 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. For instance for JBoss or other opensource products Hibernate is great. 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.

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".

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. Take a look at Data Source Architectural Patterns vs. Object-Relational Behavioral Patterns:

PEAA Catalog


DAO ( Data Access Object ) is part of the core J2EE patterns catalog:

The DAO pattern


This is a starter tutorial for Hibernate:

Hibernate


The official page of Toplink:

Toplink


Finally I "think" the good think of JPA is that you may change providers lately.

Start simple and then evolve.

I hope this helps.

这篇关于何时使用Hibernate / JPA / Toplink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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