Category Archives: Hibernate

Hibernate Best Practices – using sequences for bulk inserts

For many database tables, an auto increment column is used for the primary key. This is fine for inserting single values, but if you want to bulk insert hundreds or thousands of values and you need to refer to the … Continue reading

Posted in Hibernate, Java, Uncategorized | Tagged , | Leave a comment

Hibernate Best Practices – Parameterising Criteria Queries

Recently one of our database team pointed out some queries that were not running efficiently. We looked into the queries and they were Hibernate criteria queries. Consider a simple helper method that is creating a predicate for use in a … Continue reading

Posted in Hibernate | Tagged , | Leave a comment

Hibernate internals – how does pagination work?

Hibernate supports pagination for all query types – HQL, Criteria and SQL, via the query setMaxResults() and setFirstResult() methods, but how does this work under the covers? Well, assuming your database supports it, it will add a row_number() to your … Continue reading

Posted in Hibernate, Java | Tagged , | Leave a comment

Optimistic locking and versioning with Hibernate

Just put a small example of Hibernate optimistic locking and versioning on github: https://github.com/hedleyproctor/HibernateVersioningExample Hibernate uses an optimistic locking strategy, based on versioning. This means it tries to avoid taking a lock on data and instead relies on having a … Continue reading

Posted in Hibernate | Tagged | Leave a comment

Distinct, order by and Hibernate HQL

Recently someone posted a question on stackoverflow explaining that they were having a problem with a Hibernate HQL query that involved both order by and a distinct in the select. The query was across five linked entities – a many … Continue reading

Posted in Hibernate | Tagged | 1 Comment

Understanding the Hibernate session cache

When you use Hibernate, you do so via a session. It is well known that the session acts as a first level cache, but sometimes this can cause confusing behaviour. Let’s look at a simple example. Suppose I have Customer … Continue reading

Posted in Hibernate | Tagged | Leave a comment

Understanding Hibernate session flushing

When you interact with Hibernate, you do so via a Hibernate session. Hibernate sessions are flushed to the database in three situations: When you commit a (Hibernate) transaction. Before you run a query. When you call session.flush(). However, it is … Continue reading

Posted in Hibernate | Tagged | 2 Comments

Hibernate query limitations and correlated sub queries

It’s well known that one of the key limitations of writing queries in Hibernate, using either HQL or criteria, is that you cannot write queries that have a join in the from clause. What is less well known is that … Continue reading

Posted in Hibernate | Tagged | 1 Comment

Bespoke join conditions with Hibernate JoinFormula

Recently someone posted a question on stackoverflow asking how to deal with a database join, where the foreign key could reside in one of two different columns. This situation is sometimes found in a legacy database schema, where someone has … Continue reading

Posted in Hibernate | Tagged | Leave a comment

Hibernate example 4 – many to many associations

I’ve put some code on Github that shows three ways of modelling many to many associations with JPA / Hibernate: Using a join table that is not mapped as either an entity or an embedded component type. Mapping the join … Continue reading

Posted in Hibernate, Java | Tagged , | Leave a comment