Be Careful When Using Parent-Child Associations in Hibernate

Parent-child relationships are a special case of more general 1:M associations. They are simply part-whole relationships and for Hibernate it is not meaningful that parts (childs) should exist without belonging a whole (parent). Parent-child association is specified with orphanRemoval attribute of @OneToMany annotation. Hibernate achieves this by employing a special persistent collection implementation. First thing […]

Hibernate’s New Feature For Overcoming Frustrating LazyInitializationExceptions

For many years LazyInitializationExceptions have become most frustrating point of Hibernate. This exception occurs when you try to access an un-initialised lazy association of a detached entity. In general, entities become detached in three different ways; session close session clear session evict You have to be sure that any lazy attributes you will access be […]

How to build SessionFactory in Hibernate 4

Well, I think it is the first time in Hibernate’s history that it is released with incomplete feature implementations and documentation. I came to this conclusion when I see Configuration.buildSessionFactory() method as deprecated. When I look at documentation however it still uses above method to create it! If you look at org.hibernate.cfg.Configuration class, it is […]

Reassociated object has dirty collection reference

This is something really weird Hibernate error you may come up in several situations. When I first googled around, I found some sites commenting the source of error as “trying to attach a transient entity with the session, or entity with a collection not in type of PersistentCollection etc.” However, none of those cases maches […]

A Criteria Builder Idiom for Hibernate Queries

This simple idiom continuingly appears in our search/find use cases, and several other ones, which have parts, in which we find some data to act on it, according to some specific condition. In search/find use cases, users enter some data pattern, select some options, enter date ranges to narrow or broaden the result of those […]

Auto Scanning JPA Entities

Most of the time you will find JPA’s auto scan mechanism for annotated entities very limited. It only scans paths starting from parent of classpath:META-INF/ folder from which persistence.xml is loaded. If you want to use persistence.xml file located in a different place, for example in a jar, your annotated entities won’t be scanned because […]

Weird Connection Problems with Spring and Hibernate

At the beginning of this week a colleague of mine said to me that some JDBC connections were left open in one of our web projects. Before continuing to the rest of the story, let me first draw a rough architectural picture of the project. We use JPA/Hibernate and Spring transactions decleratively in data access […]

Audit Logging at Service Level

What is Service Level Auditing? Many enterprise business applications have such requirements that they should log their users’ operations; who performs and when, records that are inserted into, deleted from database, or are changed during those operations, with a meaningful description about current state of those records. Hibernate already provides an interceptor mechanism at SessionFactory level. […]

A Hybrid SessionFactoryBean

LocalSessionFactoryBean is a nice way to create and initialize a Hibernate SessionFactory instance in applications. It lets us to configure other objects and properties, such as DataSource, and other hibernate properties that SessionFactory instance depends during initialization inside Spring?s context mechanism. We can easily configure other parts of our application, that are related with SessionFactory […]

A Small Silly Reminder For Unclosed Session Warning of Hibernate

If you suddenly get a warning, saying that ?unclosed connection, forgot to call close() on your Session? from Hibernate and you employ OpenSessionInViewFilter to manage your Hibernate sessions, check that your OpenSessionInViewFilter?s filter mapping in web.xml comes before any other filter mappings, for example Acegi Security Filters, especially! Because, those other mappings may trigger some […]