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.

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.

Mckoi SQLorg.hibernate.dialect.MckoiDialect
Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialect
Oracle 9org.hibernate.dialect.Oracle9Dialect
SAP DBorg.hibernate.dialect.SAPDBDialect
Sybase Anywhereorg.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


Hibernate and concurrency

Hibernate & Concurrency: The Data Knowledge Stack