Fixing Duplicate Data from Hibernate Queries

Fixing Duplicate Data from Hibernate Queries

Author: Trevor Page: Java Guru | Programmer | Teacher September 16, 2014 Duration: 31:48

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 User and it joins to another table/object in an optional One-to-Many or optional Many-to-Many format, then you may get duplicates.

Consider this scenario… A User objects joins to the LoginHistory object, which keeps track of all the times a particularUser has logged into the system. And let's say our user has logged in many times. You'll have a situation where you have many records in the LoginHistory table.

So what happens when you run a query that joins to the LoginHistory table? Well it will return as many rows as there are entries for that User in the LoginHistory table.

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:

  1. Declare your joining object as a Set
  2. Make use of Distinct Root Entity Results Transformer

Click Here to Keep Reading


The How to Program with Java Podcast is a practical guide for anyone wanting to build software, from curious beginners to those looking to solidify their professional skills. Host Trevor Page, a seasoned Java guru and teacher, breaks down complex programming concepts into clear, manageable lessons you can apply immediately. You'll hear detailed walkthroughs on building applications, understanding core principles like object-oriented design, and navigating the tools of the trade. The discussions move beyond dry theory, focusing on the real-world logic and problem-solving that defines a career in software development. Whether you're aiming for a new job, tackling a personal project, or helping a child explore technology, this podcast provides a structured, conversational path forward. Each episode is crafted to demystify the process, translating technical jargon into plain English so you can focus on creating functional code. The tone is direct and encouraging, rooted in Trevor's experience as both a programmer and an educator. It’s a resource for building a tangible skill set, episode by episode, where consistent listening translates directly to growing competency. You’ll find the content naturally bridges the categories of technology education and career development, making it a useful companion for your learning journey.
Author: Language: English Episodes: 113

How to Program with Java Podcast
Podcast Episodes
Database Relationships – One to Many [not-audio_url] [/not-audio_url]

Duration: 32:16
We've talked about relational databases already, and we've learned why this type of database management really dovetails with the object oriented programming model. So now I want to dive into the specifics when it comes…
Database Terminology - Relationships, Joins and Keys [not-audio_url] [/not-audio_url]

Duration: 0:00
Terminology It's the foundation when learning any new concepts. In this episode of the "How to Program with Java Podcast" we will be talking about some new database terminology. One of the most important aspects of moder…
CRUD Operations - Syntax for Interacting with your Database [not-audio_url] [/not-audio_url]

Duration: 55:16
What's all this CRUD about? Create Read Update Delete This is the at the heart of all databases and SQL. A database essentially carries out these four operations over and over again for the duration of its existence. In…
Intro to Databases and SQL [not-audio_url] [/not-audio_url]

Duration: 0:00
Ladies and gentlemen the time has come for you to start learning about Databases and SQL. In this episode I will talk about the very basics of databases and why it is they exist in the first place. You'll learn things li…
Let's talk AJAX - It's not just for cleaning anymore! [not-audio_url] [/not-audio_url]

Duration: 32:15
AJAX (Asynchronous Javascript and XML) is a techonology used to create Rich Internet Applications (RIA). If you're asking "what the heck does that mean!?" You're in the right place In this podcast episode we will talk ab…
Intro to Algorithms and Big-O Notation [not-audio_url] [/not-audio_url]

Duration: 54:12
An algorithm is essentially a well defined set of instructions that get carried out by a computer in an automated fashion to solve a problem. A good example of this is to say "How would you tell a computer to figure out…
Spring Security [not-audio_url] [/not-audio_url]

Duration: 49:37
Ever wondered how you could properly restrict access to certain parts of your web application? Do you have an application that has a user registration page and a user login page? Spring Security is the answer if you have…
Data Binding with Spring Framework [not-audio_url] [/not-audio_url]

Duration: 42:43
So, the Spring framework is a wonderful help for programmers, but sometimes it needs a bit of help. It does a good job of trying to match fields on a webpage to Java objects (aka Java Beans), but sometimes when the "mapp…
Spring Framework - Fun with Controllers [not-audio_url] [/not-audio_url]

Duration: 55:07
In our last episode I talked about controllers and how they are used in Spring. In this episode I will be diving even further into Controllers in the Spring Framework and I will talk about the different parameters that c…
Intro to the Spring Framework [not-audio_url] [/not-audio_url]

Duration: 49:17
Well, so far we've been focusing on a bunch of topics that align themselves around the Java programming language in general. But now we're going to take things into a slightly new direction, we're going to be talking abo…