Mon, 15 September 2014
How to Fix Duplicate Data from Hibernate Queries
This problem was the bane of my existence when I first started using Hibernate because I had no idea where the problem was coming from.
If you’re executing a query and finding that you have a bunch of duplicate records and haven’t a clue why, then you’re in the right place.
You see the problem is typically caused by having left joins (or optional joins) in your objects. When you have a base object, like say
Consider this scenario… A
So what happens when you run a query that joins to the
So because of this, Hibernate doesn’t massage the data for you, it just returns exactly what it got from the database. The ball is in your court to tell Hibernate what to do with records it has retrieved.
There are two solutions to this problem:
Fri, 5 September 2014
Now it’s time to dive into the nitty gritty of Hibernate’s mysterious inner workings.
I’m by no means an expert in Hibernate, but I do use it almost every day for my own projects, so I do know a thing or two about how it works.
One topic that had me scratching my head for ages was the Hibernate life cycle. What I mean by the life cycle is the way Hibernate interacts with Java objects at certain points in the existence of said Java objects.
Let’s start from the beginning…
What the heck is a Hibernate Life Cycle?
You see, Hibernate is picky about your Java objects. Hibernate prefers your objects to be in a certain “state”, known as the persistent state… this persistent state is one of four different states that exist inside of the hibernate persistence life cycle.
Once you have a firm grasp of the different states that an object can be in (as it pertains to Hibernate) you’ll be well on your way to mastering the Hibernate framework.
So let’s get this Hibernate persistence life cycle lesson started shall we?
Continue reading via: http://howtoprogramwithjava.com/session58