Wednesday, April 25, 2007

Using Hibernate for MySQL

Charles Lee is presenting the session "Using Hibernate to Ease the Migration to Open Source Databases."

Most of the talk so far is Hyperic related.

EJBQL is a poor query language and has bad transaction handling. It also has pessimistic locking and lack of database tools. Entity Beans are proxy objects. Managers interface with entity beans.

Why migrate to Hibernate?

HQL and Criteria based queries supported by Hibernate are fully featured. It does Lazy fetching and doesn't loads objects until you need them. There is a straight forward transaction demarcation. it has secondary cache integration and is a pretty popular framework.

Hibernate POJOs and Transactions: Hibernate POJOs are actually detached from the framework and don't have to inherit the framework API. They are actual objects that can be passed around. Managers look up the POJOs and POJOs travel through the transaction boundaries. Hibernate sessions use optimistic locking. Doesn't lock anything until you flush session and reconnect. Integration with secondary cache to cache both POJOs and query results.

The query languages:
EJBQL: lacks trivial functions like "ORDER BY"

JBossQL: Some additional functions like "ORDER BY", "LCASE" etc

DeclaredSQL: Direct SQL: like queries declared as EJB finder methods, but not database-specific.

Direct SQL: When you hit a wall in application, they go to DirectSQL.

HQL (and Criteria API): It is close to SQL, object oriented and allows query results to be paged. You get all functions you expect like "Order by". You can take advantage of database specific SQL.

Some slides about HQL vs. Criteria API.

Hibernate supports MySQL out of the box. You can use hibernate's hbm2ddl to create database schema. It hooks into Hibernate's dialects to generate database specific DDL. You can also use own DBUtil to populate database initially.


Anonymous said...

I use hibernate/mysql on my website and i appreciate this technology. tank you for your article.

K said...

I got connection exception & resolved the issue after 3 hectic days.Check if you are using I hibernate3. In this version it is req to explicitly mention the connection class name. Also check if the jar is in classpath. Check steps & comments in below link

Remove autoReconnec=true