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
EP 63: HTML Styles 101 [not-audio_url] [/not-audio_url]

Duration: 48:48
In this HTML tutorial I would like to introduce two concepts to you: DOCTYPE Styles The DOCTYPE Currently as of the date this was published, web browsers are (mostly) supporting HTML 5. HTML has gone through several vers…
EP 62: Introduction to HTML [not-audio_url] [/not-audio_url]

Duration: 34:12
I'm so ridiculously excited to be introducing a whole new aspect of programming. Over the next few months I will be posting new articles on the topics of HTML, Javascript and jQuery. These are all technologies that you c…
HTML, CSS and JavaScript - Oh My! [not-audio_url] [/not-audio_url]

Duration: 40:40
The presentation layer is one of the three main layers in object-oriented programming. The three main layers include: Presentation Layer Business Layer Data Layer Now, these three layers are typically related to enterpri…
Hibernate Group By [not-audio_url] [/not-audio_url]

Duration: 43:05
In our last Hibernate lesson, you learned how to fix duplicate data from hibernate queries. In this lesson we're going to focus on some of theaggregate functions that can be used with Hibernate, and of course that means…
Hibernate Persistence Life Cycle [not-audio_url] [/not-audio_url]

Duration: 42:08
Now it's time to dive into the nitty gritty of Hibernate's mysterious inner workings. I'm by no means an expert in Hibernate, but I do use it almost every day for my own projects, so I do know a thing or two about how it…
Fetch Type Lazy Vs Eager [not-audio_url] [/not-audio_url]

Duration: 29:13
What the heck is a Fetch Type? Great question! Hibernate is a very handy framework for removing your need to fully understand SQL, but it will force you to understand things such as joins. Joining two tables in SQL is th…
Hibernate @ManyToMany Unidirectional and Bidirectional [not-audio_url] [/not-audio_url]

Duration: 53:00
Hibernate @ManyToMany Unidirectional The Many-to-Many relationship can be best described by example. The example we're going to use is that of the relationship between an Author and a Book. Authors publish Books, and Boo…
Hibernate @OneToOne Unidirectional / Bidirectional [not-audio_url] [/not-audio_url]

Duration: 36:21
One-to-One Unidirectional Relationship Since you've already learned about the ins and outs of how unidirectional one-to-many and bidirectional one-to-many relationships work, it's time to learn about the One-to-One relat…
Hibernate @OneToMany Bidirectional Relationship [not-audio_url] [/not-audio_url]

Duration: 34:09
Since we've already learned about the unidirectional @ManyToOne relationship, we can now move on to talking about what a bidirectional relationship is like, when using Hibernate. The term "bidirectional" literally means…
Mapping Relationships with Hibernate [not-audio_url] [/not-audio_url]

Duration: 55:48
In the past we have learned about database relationships, specifically the One-to-Many as well as the Many-to-Many and One-to-One and that was all good, great and grand… But now I want to talk about how to create those s…