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
Java Observer, Observable Design Pattern [not-audio_url] [/not-audio_url]

Duration: 40:18
Today let's talk about some design patterns, namely the Observer design pattern which can be used in Java. Following the Oberser design pattern in Java is as easy as extending the Observable object and implementing the O…
Interview w/ Self-Taught Programmer Troy Davis [not-audio_url] [/not-audio_url]

Duration: 1:14:51
In this epsidoe of the How to Program with Java podcast, I talk with a self-taught programmer who has started up his own very successful marketing product based on his programming knowledge. Troy was graduated with a deg…
The Agile Development Methodology [not-audio_url] [/not-audio_url]

Duration: 42:17
If you've worked as a developer for a corporation, you've likely been exposed to the waterfall model of software development... If not, consider yourself lucky. I've worked in this model before and it had its issues, but…
Alan Robbins - Self-taught Programmer Success Story [not-audio_url] [/not-audio_url]

Duration: 43:52
Alan Robbins is a self-taught programmer who dabbled with programming in college. After not doing to well in the post-secondary system with respect to programming, he changed his major and graduated with a degree in Busi…
The Software Development Life-Cycle [not-audio_url] [/not-audio_url]

Duration: 52:11
Knowing how to program with Java is only some of the battle when it comes to developing software, the other major part of the battle is knowing HOW to distribute the software. There are entire schools of thought that sur…
Getting a Job as a Self-Taught Programmer [not-audio_url] [/not-audio_url]

Duration: 38:44
Landing a job as a self-taught programmer is not impossible, and I will have an in-depth conversation about why this is the case. I will talk about strategies like creating your own software product based on your own int…
Encapsulation and Final Keyword in Java [not-audio_url] [/not-audio_url]

Duration: 39:55
Learn all about one of the four major principles of Object Oriented Programming (encapsulation) as well as the "final" keyword. In this episode I also dive into the popular topic of Java Certification. Learn what certifi…
Java Interview Question - Part II [not-audio_url] [/not-audio_url]

Duration: 38:20
In this session I will cover some additional Java interview questions that you should be able to answer. I also cover some general tips that you should keep in mind when being interviewed (as it's not just these question…
Java Interview Questions Part I [not-audio_url] [/not-audio_url]

Duration: 45:51
This is part I of a series of episodes dedicated to common questions that are asked in entry level Java interview situations. The questions covered in this episode are some basic questions that I uncovered across the int…