Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Hibernate/JPA/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.

like image 873
GBa Avatar asked Sep 23 '08 17:09

GBa


Video Answer


3 Answers

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.

like image 151
OscarRyz Avatar answered Sep 19 '22 00:09

OscarRyz


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.

like image 32
ColinD Avatar answered Sep 22 '22 00:09

ColinD


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.

like image 36
Hank Avatar answered Sep 21 '22 00:09

Hank