何时使用 Hibernate/JPA/Toplink?

发布于 2024-07-06 03:31:04 字数 214 浏览 9 评论 0原文

现在我正在制作一个非常简单的网站——大约 5 页。 问题是集成某种数据库映射解决方案是否太过分并且值得花时间,或者仅使用普通的旧式 JNDI 是否会更好。 我可能需要从数据库中读取/写入十几个内容。 我想我对这些技术已经有了基本的了解,但仍然需要大量参考文档。 之前还有其他人面临过这个决定吗?

编辑:抱歉,我应该指定 JNDI 来查找数据库连接,并指定 JDBC 来执行操作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岛徒 2024-07-13 03:31:04

简短的回答:这取决于您想要支持的复杂性。

长答案:

首先,ORM(对象关系映射 - 您所说的数据库映射 - )和 JNDI(Java 命名和目录接口)是两个不同的东西。

正如您所知,第一个用于将数据库表映射到类和对象。 第二个是提供资源的查找机制,它们可能是DataSource、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 = ?
insert into employee ( bla, bla, bla ) values ( ? , ? , ? )
update etc. etc 

When ( 和 If )您的应用程序变得过于复杂,您可能会更改 DAO 实现。 例如,在“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;
}

这只是一个例子,在现实生活中你可以让抽象工厂创建 ORM DAO,但那是题外话。 要点是您可以从简单的开始,通过使用设计模式,您可以在以后需要时更改实现。

当然,如果您想学习技术,甚至可以从一张桌子开始。

选择一种或另一种(ORM 解决方案)基本上取决于您所使用的技术。 例如,对于 JBoss 或其他开源产品来说,Hibernate 就很棒。 它是开源的,有很多资源可供学习。 但是,如果您正在使用已经具有 Toplink 的东西(例如 oracle 应用程序服务器),或者如果基础已经构建在 Toplink 上,那么您应该继续使用该框架。

顺便说一句,自从 Oracle 收购了 BEA 以来,他们表示他们正在用现在称为“Oracle Weblogic 应用服务器”的 toplink 取代 Kodo(weblogic 持久性框架)。

我给您留下了一些资源,您可以在其中获取更多相关信息:


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 目录


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

DAO 模式


这是 Hibernate 的入门教程:

Hibernate


Toplink 官方页面:

Toplink


最后,我“认为”JPA 的好处是您最近可能会更改提供商。

从简单开始,然后发展。

我希望这有帮助。

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.

两个我 2024-07-13 03:31:04

学习 ORM 的最佳方式是通过一个小项目。 开始这个项目。

一旦掌握了它的窍门,您就会使用 ORM 来完成所有事情。

对于 ORM 来说,没有什么是太小的了。 在完成前几个项目后,您会发现您无法以任何其他方式工作。 ORM 映射通常比几乎任何其他工作方式都更有意义。

The best way to learn ORM is on a small project. Start on this project.

Once you get the hang of it, you'll use ORM for everything.

There's nothing too small for 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.

习ぎ惯性依靠 2024-07-13 03:31:04

你的意思是普通的旧式 JDBC 吗?
一个小项目可能是学习 ORM 框架之一的好机会,特别是如果您有时间的话。

然而,如果没有更多信息,就很难以某种方式提供建议。

Do you mean plain old JDBC?
A small project might be a good opportunity to pick up one of the ORM frameworks, especially if you have the time.

Without more information it's hard to provide a recommendation one way or another however.

淑女气质 2024-07-13 03:31:04

我的经验法则是,如果它是只读的,我愿意在 JDBC 中进行,尽管我更喜欢使用带有 SQLQuery 的空 Hibernate 项目来利用 Hibernate 的类型映射。 一旦我必须进行写入,我就会使用 Hibernate,因为设置一些属性然后调用保存比单独设置每一列要容易得多。 当您必须开始优化以避免对未更改的对象进行更新时,最好使用 OR/M 及其脏检查。 处理外键关系是您需要映射一次然后使用 getter 的另一个标志。 同样的逻辑也适用于 Toplink,尽管除非他们在我使用 HQL 的三年内添加了类似 HQL 的东西,否则 Hibernate 对于这种从纯 SQL 的过渡会更好。 请记住,您不必映射每个对象/表,只需映射具有明显优势的对象/表即可。 根据我的经验,大多数不使用现有 OR/M 的项目最终都会构建一个新的 OR/M,这是一个坏主意。

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. 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. 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. Dealing with foreign key relationships is another sign that you need to map it once and then use the getters. 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. 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.

秋凉 2024-07-13 03:31:04

对于一个非常简单的应用程序来说,这似乎有点过分了,特别是如果您没有计划对其进行扩展的话。 然而,将它们与这个简单的应用程序一起使用似乎也是值得的,这样您就可以更好地了解它们如何工作,以便下次您有可以使用它们的东西。

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.

一影成城 2024-07-13 03:31:04

看看这里的各种顶级链接指南,它们有介绍、示例、场景等

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

Look at the various toplink guides here, they have intro, examples, scenarios etc

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文