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
EP26 - IF Statements in JavaScript [not-audio_url] [/not-audio_url]

Duration: 19:23
Show notes for this episode can be found via http://coderscampus.com/26 Also don't forget to check out our lifetime membership to Coders Campus and get access to 130+ hours of video tutorial content on full-stack Java. C…
EP25 - JavaScript Data Types [not-audio_url] [/not-audio_url]

Duration: 27:45
Checkout the show notes of today's episode via http://coderscampus.com/25 Also, I've started a brand new "lifetime membership" deal for Coders Campus, you can check out the details of this new offer via http://coderscamp…
EP23 - JavaScript Variables [not-audio_url] [/not-audio_url]

Duration: 15:00
Show notes for this episode are available via http://coderscampus.com/23 Also, don't forget about the sweet deal I have over at http://coderscampus.com/deal - you help support the show when you join our community of code…
Coders Campus EP21 - Dynamic Typing in JavaScript [not-audio_url] [/not-audio_url]

Duration: 21:01
Don't forget to check out this sweet deal on a Coders Campus via http://coderscampus.com/deal You'll get access to over 128 hours of video tutorial content that will teach you how to become a full-stack programmer (inclu…
What is JavaScript? [not-audio_url] [/not-audio_url]

Duration: 22:54
Don't forget to check out the special deal that we're putting on for our podcast listeners via http://coderscampus.com/deal
EP68 - Subscribe to my new show called the "Coders Campus Podcast" [not-audio_url] [/not-audio_url]

Duration: 2:59
You can subscribe to the new show via this link: http://coderscampus.com/itunes There will be no more episodes of the How to Program with Java podcast (this podcast), all new episodes will be shared via the Coders Campus…
Episode 66 - Summer is here and something new is in the air [not-audio_url] [/not-audio_url]

Duration: 5:36
I'm happy to announce that dropping on July 1st, 2016 - a brand new podcast will be hitting the digital shelves! Although the How to Program with Java podcast will be coming to an end, I'll be launching a brand new serie…
EP65: HTML Design & Layout [not-audio_url] [/not-audio_url]

Duration: 47:01
Okay so you know how to add some styles and CSS to your webpage, that's a great start, but what about deciding where all your text should go? What if you wanted to setup your website so that you have some text on the lef…
EP 64: CSS 101 Tutorial [not-audio_url] [/not-audio_url]

Duration: 44:57
Cascading Style Sheets are a bit of a tricky beast in HTML. Given this fact, I wanted to dedicate an entire section to just this topic. Let's start with the basics, shall we? What are Cascading Style Sheets (CSS)? Well,…