Fixing Duplicate Data from Hibernate Queries

Fixing Duplicate Data from Hibernate Queries

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

Jaksot(112)

Arrays in Java

Arrays in Java

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 Bootc...

29 Marras 201233min

Methods in Java

Methods in Java

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 Bootc...

20 Marras 201223min

Java Hello World

Java Hello World

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 Bootc...

31 Loka 201226min

Syntax and Tools in Java

Syntax and Tools in Java

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 Bootc...

14 Loka 201225min

Data Structures in Java

Data Structures in Java

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 Bootc...

4 Loka 201229min

What are Control Structures in Java?

What are Control Structures in Java?

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 Bootc...

27 Syys 201228min

Variables in Java

Variables in Java

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 Bootc...

22 Syys 201231min

Intro - Who is Trevor Page?

Intro - Who is Trevor Page?

Here's episode 1 of the How to Program with Java Podcast. I'm very excited to kick off this podcast and can't wait to see where this journey takes me! Who am I? - Trevor Page - 38 Years old ...

18 Syys 201219min

Suosittua kategoriassa Liike-elämä ja talous

sijotuskasti
mimmit-sijoittaa
rss-rahapodi
psykopodiaa-podcast
rss-sisalto-kuntoon
ostan-asuntoja-podcast
rss-lahtijat
rss-startup-ministerio
inderespodi
pomojen-suusta
rss-rahamania
rss-h-asselmoilanen
herrasmieshakkerit
taloudellinen-mielenrauha
sijoituspodi
rss-vaikuttavan-opettajan-vierella
rss-seuraava-potilas
io-techin-tekniikkapodcast
bakkari-tarinoita-tapahtumien-takahuoneista
rss-tyoelamasta-podcast