Tag Archives: Hibernate

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 | 1 Comment

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 | Leave a 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

Hibernate example 3 – one to one associations

Just put a Hibernate example of the three different ways to create one-to-one associations on github: https://github.com/hedleyproctor/hibernate-one-to-one It contains a Customer entity, which has one-to-one associations to a UserProfile, MarketingPreferences and a Wistlist. Foreign key relationship. Customer has a foreign … Continue reading

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

Hibernate example 2 – one-to-many associations

Just put an example of how to use one-to-many associations with Hibernate on Github: https://github.com/hedleyproctor/hibernate-one-to-many Shows two examples of a one-to-many relationship between entities: Unordered one-to-many between Customer and Address. Ordered one-to-many between Customer and PaymentCard. The unordered association uses … Continue reading

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

Hibernate example 1 – inheritance and polymorphism

Have just put an example on Github of how to handle inheritance and polymorphism with Hibernate: https://github.com/hedleyproctor/hibernate-polymorphism It demonstrates the four ways of dealing with inheritance and polymorphism: Implicit polymorphism – no explicit mapping of the inheritance, but Hibernate can … Continue reading

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