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
EP55 - How to Sort with Streams in Java [not-audio_url] [/not-audio_url]

Duration: 37:53
In this episode we'll talk about how to sort a stream of objects using the ".sorted()" function. We'll also talk about the differences between using a stream to sort vs using something like Collections.sort(), which has…
EP54 - What is the Map Operation in Java Streams? [not-audio_url] [/not-audio_url]

Duration: 41:49
Big announcement: today marks the launch of our brand new "Beginners only" Coding Bootcamp. If you're a beginner to coding and have spent less than about 6 months learning to code, you're a great fit for this new 16 week…
EP53 - Intro to Streams in Java [not-audio_url] [/not-audio_url]

Duration: 43:44
GitHub link here: https://github.com/tp02ga/FunWithStreams In this episode we'll talk about: What are streams and why you should care about them Comparing a simple Stream example to a Looping example Intermediate vs Term…
EP52 - Most Common Functional Interfaces in Java [not-audio_url] [/not-audio_url]

Duration: 53:25
In this episode we'll talk about 7 common Functional Interfaces that Java provides to us. UnaryOperator, BinaryOperator, Supplier, Consumer, Function, Predicate and BiPredicate Interested in starting your coding career?…
EP51 - Let's Talk Lambdas in Java [not-audio_url] [/not-audio_url]

Duration: 57:32
In this episode we'll talk about a super useful feature that was introduced back in Java version 8, known as Lambdas. The Lambda feature is something you didn't know you desperately wanted or needed until you understood…
EP50 - #3 of 3 to Getting a Job as a Coder [not-audio_url] [/not-audio_url]

Duration: 31:04
In this episode we'll talk about the 3rd key to getting a job as a coder... How do you stand out from the crowd? You put in all this effort to learn how to code. You likely have a plan, you follow a curriculum, you seek…
EP49 - Top 3 mistakes coders make when applying to their first job [not-audio_url] [/not-audio_url]

Duration: 35:49
In this episode we'll talk about 3 big mistakes that aspiring coders make when trying to land their very first job. Getting your first job as a coder will be the most difficult challenge when trying to launch your profes…
EP47 - Key #2 of 3 to Getting a Job as a Coder [not-audio_url] [/not-audio_url]

Duration: 37:38
Interested in starting your coding career? I'm now accepting students into an immersive programming Bootcamp where I guarantee you a job offer upon graduation. It is a 6 month, part-time, online Bootcamp that teaches you…