Search Entity by Current Version as well as Previous Version

A search function for a management software was supposed to be able to lookup a customers data by searching with their address. Addresses can change and if for example a customer moved and thus their address changed, it should still be possible to find said customer with their old address. This functionality was required for some other entities as well, so implementing it generally was favorable. Instead of explicitly modeling for every such entity that it can have multiple values over time, I used an extensions for the Hibernate persistence framework called Envers for auditing tables. Which effectively means that an update to any row in an audited table will automatically insert the old row in an extra table which contains all previous versions of the row. Now instead of just searching the address table with the current addresses, I could also search all previous address versions in the respective auditing table. Thus being able to find a customer by its old address.

Two options for fetching data are possible: FetchType.EAGER and FetchType.LAZY. Collections are loaded EAGER when they are fetched fully at the time their parent is fetched. That is, with nested classes the whole class hierarchy is fetched, even if all the subentities (children) are not needed.

With the LAZY option, data is loaded on-demand, e.g. when a certain entity is requested explicitly. Thus, when the parent entity is loaded with the lazy option, the children entities are not loaded per default. Only in case they are really needed (e.g. a certain method was called via GUI).
Here is an example how entities are marked to be loaded lazily:

public class Parent {

private String id;

private String name;

@OneToMany(fetch = FetchType.LAZY)
private List children;

// etc.


Bad performance with nested classes and large amount of data when using Hibernate

In my JEE application, I am using the Hibernate framework as the ORM mapper. Initially, the performance was very good. However, as more and more data is gathered during the lifetime of the application, the performance is getting worse and worse. The data model is rather complicated, and the entities are nested. Are there configuration options, that could help to improve the performance?

Performance monitoring of a cloud deployment

An application go-live took a wrong turn - a dozen of connected clients took a cloud system down, that should cope with thousands of them. As I was assigned to analyze the problems I discovered that this is a composite problem created by (amongst other things) careless usage of Hibernate (an ORM framework for easier, vendor-agnostic database access) and way to much complex logic built 'into' the database - killing both the database and the web applications host by using up its CPU. After using Hibernate statistics for analyzing the transactions that are being sent to the database, I started by rewriting some SQL queries that didn’t perform well. Some complex Java/Hibernate logic even had a kind of memory leak in it which could be resolved by writing a single database stored procedure. A connectivity checking method for the clients was moved from saving ping data in the database to an efficient in memory solution using an in-memory data grid called Hazelcast. Nevertheless the CPU load stayed above an acceptable threshold. Using an application performance management tool I could find another root cause in a flawed function that was being called periodically. Looking at the CPU load graph one could see that the CPU went about each three minutes from under 10 to almost 100 percent. The load was created by one single thread with about 80% of network IO and 20% code execution, where multiple database calls were made, responses sorted and analized. Not only was the sorting flawed, also the database calls were being performed for all rows in one table and not just for a small subset of the entries. After a rewrite the CPU usage dropped to a single-digit percentage which kept the system in a healthy state.

use requires new for synchronous code that asynchronous code needs to access

the thread does not see the record because the test runs inside a transaction that is not committed yet. since the transaction is bound to the executing thread the task that is forked does not use the same transaction. in order for this to work the insert must run in a method that is annotated with @Transactional(propagation = REQUIRES_NEW). please note that transactional rollback will not work then though

CRUD operations example using Spring, MVC, Hybernate and Maven

I need an example to better understand of how to operate with these technologies. The interface will be HTML-based. The application will support all CRUD operations: create, read, update, delete. MySQL was used for the database.

Searching Entities with Hibernate Entity Manager

Actually it seems that I was working with an outdated version of Hibernate. In a new release from summer 2008 (Hibernate Entity Manager 3.4.0) they solved the problem, that entities could not be found when the path contained whitepaces, along with some other problems. Therefore the easy solution is to update your current Hibernate version to at least version 3.4.x.


Searching Entities with Hibernate Entity Manager

Hibernate is used to easily handle database operations in Java applications. It makes mapping between Java objects and the database a lot easier. It is also possible to use the persistence API (JPA) to store entities in databases. However, if you search for entities, that you want to use in your application, the path to those entities should not contain a whitespace. Otherwise you’ll get a “File not Found” – exception (this is not correct).
Subscribe to Hibernate