Custom Audit Log With Spring And Hibernate

July 18, 2016

If you need to have automatic auditing of all database operations and you are using Hibernate…you should use Envers or spring data jpa auditing. But if for some reasons you can’t use Envers, you can achieve something similar with hibernate event listeners and spring transaction synchronization. First, start with the event listener. You should capture […]

3

Spring-Managed Hibernate Event Listeners

July 15, 2016

Hibernate offers event listeners as part of its SPI. You can hook your listeners to a number of events, including pre-insert, post-insert, pre-delete, flush, etc. But sometimes in these listeners you want to use spring dependencies. I’ve written previously on how to do that, but hibernate has been upgraded and now there’s a better way […]

0

Setting Up Distributed Infinispan Cache with Hibernate and Spring

February 17, 2016

A pretty typical setup – spring/hibernate application that requires a distributed cache. But it turns out not so trivial to setup. You obviously need cache. There are options to do that with EhCache, Hazelcast, Infinispan, memcached, Redis, AWS’s elasticache and some others. However, EhCache supports only replicated and not distributed cache, and Hazelcast does not […]

3

A Tip For Properly Utilizing Hibernate 2nd Level Cache

October 27, 2012

Your entity is @Cacheable, your cache is configured properly, but still, loading an entity produces a lot of sql queries. I noticed that the other day, and realized something: hibernate does not cache collection mappings. So if you have @OneToMany, they are fetched from the database. Which is a bit counter-intuitive, because my assumption was […]

1

How To Generate A Schema Creation Script With Hibernate 4, JPA And Maven

July 26, 2012

The scenario is trivial – you want to generate a database schema creation script while building your application (and then execute the script on the target database) This was relatively easy with Hibernate 3, as there was the hibernate3-maven-plugin, but it is not compatible with Hibernate 4. And for every new project you should start […]

4

A Hibernate Session May Get Flushed When Executing Queries

January 18, 2012
Tags:

Today I noticed this Hibernate stactrace: This means that when calling Query#list() (same goes for the JPA variant, which simply invokes the hinbernate class), the session might be flushed. That actually makes sense, because you may have: This will return the newly inserted foo only if the session gets flushed. Otherwise the changes will not […]

0

Avoid Lazy JPA Collections

October 25, 2011
Tags: ,

Hibernate (and actually JPA) has collection mappings: @OneToMany, @ManyToMany, @ElementCollection. All of these are by default lazy. This means the collections are specific implementations of the List or Set interface that hold a reference to the persistent session and the values are loaded from the database only if the collection is accessed. That saves unnecessary […]

24

Spring-managed Hibernate Listeners with JPA

September 28, 2011

Update: now there’s a better way to do that. A standard use-case – you need an entity listener in order to execute some code on every update/insert/delete. For auditing, for example. But things are not straightforward if you need spring dependencies in your listeners and you are using JPA. First of all, the JPA-only listeners […]

10

How to Map Dates with Hibernate – Use joda-time

September 5, 2011

This is always a question – how to map temporal data in our hibernate entities – whether to use java.util.Date, java.util.Calendar or simply long. The correct answer is: neither of these. Use joda-time – the de-facto Java datetime API. Using it throughout the whole project is a no-brainer, but how to use it with hibernate […]

4

Some Technical Details About My Startup

April 12, 2011

Today I launched a “field trial” of my sort-of-a-startup (http://welshare.com). And here I’ll write some technical details. The stack: Java – the language and the platform I’m most familiar with. It is claimed that the JVM is very performant, so that’s welcome. spring – all internal functioning of the application is wired by the spring […]

10