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
EP36 - Objects in JavaScript [not-audio_url] [/not-audio_url]

Duration: 20:36
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…
EP35 - JavaScript TypeOf Keyword [not-audio_url] [/not-audio_url]

Duration: 14:58
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…
EP 34 - Getting and Showing Data from Users [not-audio_url] [/not-audio_url]

Duration: 13:22
Show notes for this episode are available via http://coderscampus.com/34 Don't forget to check out our current "deal" for coders campus available via http://coderscampus.com/deal
EP33 - Importing JavaScript into HTML [not-audio_url] [/not-audio_url]

Duration: 17:46
Show notes for this episode are available via http://coderscampus.com/33 Don't forget to check out our current "deal" for coders campus available via http://coderscampus.com/deal
EP32 - Debugging in JavaScript [not-audio_url] [/not-audio_url]

Duration: 24:30
Show notes for this episode are available via http://coderscampus.com/32 Don't forget to check out our current "deal" for coders campus available via http://coderscampus.com/deal
EP31 - Functions in JavaScript [not-audio_url] [/not-audio_url]

Duration: 28:56
Show notes for this episode are available via http://coderscampus.com/31 Don't forget to check out our current "deal" for coders campus available via http://coderscampus.com/deal
EP30 - Landing the interview, a coder's guide to applying for jobs [not-audio_url] [/not-audio_url]

Duration: 31:05
Show notes are available via http://coderscampus.com/30 If you're interested in grabbing the new "Java Interview Prep Course" you can get a significant discount by joining the "pre-sale" version of the course before Dec.…
EP29 - While Loops in JavaScript [not-audio_url] [/not-audio_url]

Duration: 18:35
Show notes for this episode are available via http://coderscampus.com/29 Don't forget to check out our current "deal" for coders campus available via http://coderscampus.com/deal
EP28 - For Loops in JavaScript [not-audio_url] [/not-audio_url]

Duration: 21:47
Show notes for this episode can be found here: http://coderscampus.com/28 If you're interested in supporting the show, please leave a rating and review via http://coderscampus.com/itunes And if you are interested in taki…