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
Hibernate - Creating Data Access Objects (DAOs) [not-audio_url] [/not-audio_url]

Duration: 59:29
Data Access Objects – What are they? Data Access Objects (or DAOs for short) are used as a direct line of connection and communication with our database. DAOs are used when the actual CRUD (CRUD = Create, Read, Update, D…
Hibernate's most important annotation - @Entity [not-audio_url] [/not-audio_url]

Duration: 59:24
What You'll Learn The focus of this podcast / blog post is to teach you how to create the connection between your Java objects and the SQL Database tables. Remember, the whole point of the Hibernate framework is for you…
Hibernate Persistence for Beginners [not-audio_url] [/not-audio_url]

Duration: 1:01:10
What is a Persistence Framework? As the name implies, it has something to do with persisting things... this means that we're still talking about databases. But what is it that we are persisting with this framework? Objec…
The SQL Subquery [not-audio_url] [/not-audio_url]

Duration: 48:11
The SQL Subquery Now that you've learned about SQL Joins, aggregate functionsand the group by keyword, it's time we moved on to our final topic in our SQL tutorial series. Today you'll be learning all about SQL Subquerie…
SQL Group By [not-audio_url] [/not-audio_url]

Duration: 47:00
After having talked about all the SQL Aggregate functions, there's one more topic that goes hand in hand with what we've already learned… The group by keyword. This particular keyword allows us to take a bunch of data an…
SQL Aggregate Functions [not-audio_url] [/not-audio_url]

Duration: 32:12
In today's podcast episode you'll be learning all about the aggregate functions that exist in SQL. What the heck is an aggregate function? Well that's what I'm going to try and teach you today, and I promise, it's not a…
Database Joins [not-audio_url] [/not-audio_url]

Duration: 57:26
There are three categories of joins that you can make use of in SQL: Inner Join Outer Join Cross Join But before we dive into the categories of joins, we first need to have an understanding of what a join really is. Join…
Enforcing Database Relationships Part II [not-audio_url] [/not-audio_url]

Duration: 55:31
In this SQL tutorial episode/post we're going to learn how to enforce our SQL relationships that we've already learned about. We're going to be tackling the one-to-one and many-to-many relationships and we're going to le…
Creating Database Tables in MySQL [not-audio_url] [/not-audio_url]

Duration: 1:02:19
You've learned all about how to create sql queries to read, write, update and delete data… but you haven't yet learned how to create the tables where you'll be doing the reading, writing, updating and deleting. So that's…
Database Relationships: Many-to-Many and One-to-One [not-audio_url] [/not-audio_url]

Duration: 44:01
Show Notes available via: http://howtoprogramwithjava.com/session43 In this post we will be expanding on the topic of database relationships and touch on two that are less common but just as useful. Many-to-Many Relation…