my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes Hibernate Newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Newbie" Watch "Hibernate Newbie" New topic
Author

Hibernate Newbie

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
I used entity bean before and is considering learning Hibernate - that means i know absolutely nothing about Hibernate. Some questions:

1. Does Hibernate aim at providing database independent? Just like entity bean spec doesnt say anything on the persistence mechanism.

2. Is the performance of Hibernate good enough (at least comparing to evil entity bean)? How it achieve this (i mean is it similar to entity bean - persisting one object to 1 row of table)

3. How stored procedure fit into the Hibernate picture? I have this worry because with entity bean, there is no place for stored procedure and performance is bad.

Thanks for the advice
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
1) Yes, hibernate makes it very easy to change DB's. For the most part changing one or two lines in a config file will do it.

2) I have found the performance of hibernate to be very good. I have used it with datasets of about 3000+ rows picking about a dozen columns spread out over a few of tables (using joins, but with hibernate I didn't have to actually type in the joins).

3) I haven't used hibernate with stored procedures yet.
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Hi Steven,

Thx for your explanation. Would you mind to briefly explain how Hibernate achieve the database independent? In entity bean with CMP, the EJB container transparently calls the SQL insert/update/delete and persist each entity bean instance to 1 row in the database. So programmer dont need to bother issuing JDBC directly. But this approach also suffers from performance bottleneck as network traffic is required for each row in the table (correct me if I'm wrong).

So how Hibernate get rid of this 1 row 1 network call issue? Sorry my O/R mapping knowledge is limited so not able to image other approach to persist ohjects efficiently.

Thx for any advice.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
There are two main ways to query for objects in hibernate. The first is called a 'Criteria' query. With this you basically are creating an object and setting the fields you want to use in the search. You then pass the Criteria object to hibernate and it will translate that into sql.


The second is called hql. hql is a very sql like query language. An example would probably be the easiest to explain.

given the following class:


If you wanted you get all the Person objects out of the db you would write

from Person

and pass that to hibernate. If you wanted to get all people named John Smith you would write

from Person as p where p.name='John Smith'

These would both return a List of Person objects.
You can also just query for fields (this will often help performance when there are one to many relathionships)

select p.name, p.spouse.name, p.address, p.spouse.address from Person as p

That would return a List of Object[]'s each Object array would have 4 Strings in order of the fields listed in the select. Note that I didn't have to join a Person to it's spouse.

There is also lazy loading to increase performance. Lets say I do

from Person

to get all the Person objects. That is going to do one hit to get the list of Persons, then another hit for each person to get the spouse(commonly refered to as the N+1 problem). With lazy loading I could do the same query, but it would only hit the db once to get me all the Person objects. Then if you tried to access a spouse it would go to the db and get just that one spouse.

The hibernate pdf manual is a great resource for all this stuff.
 
Don't get me started about those stupid light bulbs.
 
subject: Hibernate Newbie
 
Similar Threads
Stored Procedure + Hibernate + DB2
Transaction Control for a method.
Hibernate and Stored Procedure
Hibernate multi row insert
EJB or Stored Procedure