Random Notes on Hibernate
An alternative to consider
If I were going to do it again now, I might not choose Hibernate for various
reasons noted at the URI below. Some of these aren't completely accurate,
Spring, for instance, overcomes in its implementation of Hibernate, some of
them, but still it bears looking into.
"Just because you're using Hibernate doesn't mean you have to use it for
everything." —Gavin King, creator of Hibernate
Hibernate is a data persistence framework for use in Java (and by code written
in still other languages).
Hibernate's purpose is to abstract schema from any database into a Java format
for consumption by code thereby isolating the business data from the actual
workings of the code.
This means that the result of a query is an instance of a class or a set or
collection of instances and not rows or tables.
BIG WARNING: You must be VERY careful when using a tutorial off the web to
learn Hibernate. There is much XML involved. Often, the authoring used to get
the article to the web substitutes crap for syntactically crucial contructions
and individual characters, especially double-quotes, single-quotes and hyphens.
If you get these wrong, an editor such as the one in Eclipse will give you
funny colors and you should realize this. However, if you do not, you'll tear
your hair out as Hibernate rejects your configuration file and mapping files.
There are a great many tutorials and other help for Hibernate.
Fast-forwarded video tutorial. Operates at lightspeed. This is amazing
and too fast. Yet, if you've begun studying Hibernate, it makes a great
deal of sense and boils everything down to a very useful 10 minutes:
The main tutorial (pre-Java 5 unfortunately):
Very good series, almost a text book, on Hibernate with a tutorial that
starts with JDBC so you can see why Hibernate.
This started out well, but in the end, the tutorial lacked code for two
of the classes.
Hibernate with MySQL—a beginner's guide, Part 1.
Hibernate with MySQL—a beginner's guide, Part 2.
First Hiberate Tutorial: Get Hands-on Experience. This one uses
Spring JPA Tutorial: Get Hands-on Experience. This one uses Eclipse.
Hibernate basics. This is a really useful article that doesn't gloss over
topics as do others.
The RoseIndia tutorial, a little hard to read, but the examples can
dispell questions you may have.
The code in this one is pretty useful too.
The following is a list of dialects Hibernate "speaks." The classpath noted is
the one to use in
hibernate.properties or hibernate.cfg.xml.
Mckoi SQL org.hibernate.dialect.MckoiDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
Oracle 9 org.hibernate.dialect.Oracle9Dialect
SAP DB org.hibernate.dialect.SAPDBDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
It would appear that it's more efficient to use < and > than <= or
>= in terms of what Hibernate will cause to happen underneath, though this
is all entirely dependent upon the database interfaced.
Hibernate's query language
Sort of like
SELECT *. For example,
returns all instances of the class
Alias; assigns a "variable" to a result by which it may subsequently be
referred. For example,
from Person as person
inner join, left outer join, etc. For example,
from Person as person
join person.spouse as spouse
left join person.children as child
As in SQL. For example,
from Person where name="Fritz"
As in SQL.
group by and having
where but for sets. For example,
from Car as car
group by car.color
having car.color in (Color.METALLIC, Color.BLACK)
Some useful database definitions
The following are useful:
join clause combines records from two or more tables. Its result is
a set that can be saved as a table or merely consumed and discarded
(as a set). A join combines fields using values common to each table. There
are four types of joins: inner, outer, left and
right. In many systems, the default join (in the absense of more
specific terminology or keywords) is the inner join.
Result obtained from combining values of two tables base on the predicate
(A.one-thing = B.another-thing), e.g.:
SELECT * FROM employee, department
INNER JOIN department
ON employee.departmentid = department.departmentid;
This is really just a formal notation for:
SELECT * FROM employee WHERE employee.departmentid = department.departmentid
...and has the result of selecting those employees who work for an
explicit, existing department.
An inner join where only 'equals' is used (and not 'greater than' or
'less than', etc.)
Where the result is created only from columns whose name matches (e.g.:
'departmentid' as opposed to 'deptid' and 'departmentid').
left- and right outer joins. All records
of the table are retained as the result (i.e.: all rows from the left table
or all from the right table).
left outer join, retains records from first table, see
right outer join, retains records from second table, see
full outer join
Retains records from both sides (tables).
Joins a table to itself.
Query by example in Hibernate
Here's an article on querying by example,
Debugging: Finding the origin of a query
Hibernate and concurrency
Hibernate & Concurrency: The Data Knowledge Stack