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.
https://github.com/l3nz/ObjectiveSync
"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.
Links
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:
http://www.youtube.com/watch?v=nLOiW9Lw4mo
http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html
http://www.hibernatetutorial.com/index.html
The main tutorial (pre-Java 5 unfortunately):
http://docs.jboss.org/hibernate/stable/core/reference/en/html/index.html
Best practices .
Portability considerations .
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
Eclipse.
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.
Hibernate dialects
The following is a list of dialects Hibernate "speaks." The classpath noted is
the one to use in hibernate.properties or hibernate.cfg.xml .
DB2 org.hibernate.dialect.DB2Dialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Informix org.hibernate.dialect.InformixDialect
Ingres org.hibernate.dialect.IngresDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
MySQL org.hibernate.dialect.MySQLDialect
Oracle org.hibernate.dialect.OracleDialect
Oracle 9 org.hibernate.dialect.Oracle9Dialect
Progress org.hibernate.dialect.ProgressDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Paging efficiency
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
See
http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html .
from
Sort of like SELECT * . For example,
from table.Person
from Person
returns all instances of the class Person .
as
Alias; assigns a "variable" to a result by which it may subsequently be
referred. For example,
from Person as person
join
inner join , left outer join , etc. For example,
from Person as person
join person.spouse as spouse
left join person.children as child
where
As in SQL. For example,
from Person where name="Fritz"
order by
As in SQL.
group by and having
Like where but for sets. For example,
select car.color
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
A 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 .
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.
equijoin
An inner join where only 'equals' is used (and not 'greater than' or
'less than', etc.)
natural join
Where the result is created only from columns whose name matches (e.g.:
'departmentid' as opposed to 'deptid' and 'departmentid').
outer join
Subdivided between 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 join
Also left outer join , retains records from first table, see
Wikipedia .
right join
Also right outer join , retains records from second table, see
Wikipedia .
full outer join
Retains records from both sides (tables).
See
Wikipedia .
self join
Joins a table to itself.
See
Wikipedia .
Query by example in Hibernate
Here's an article on querying by example,
http://java.dzone.com/articles/hibernate-query-example-qbe .
Debugging: Finding the origin of a query
http://java.dzone.com/articles/hibernate-debugging-where-does
Hibernate and concurrency
Hibernate & Concurrency: The Data Knowledge Stack