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)

EP57 - From Restaurant Manager to Software Developer

EP57 - From Restaurant Manager to Software Developer

In this episode we'll talk to Yasiin, who is a graduate from the Coders Campus Bootcamp. We dive into his story about how he started his coding journey as a Restaurant Manager, and went through two C...

27 Tammi 202239min

EP56 - Fun Tricks and Analytics with IntStreams in Java

EP56 - Fun Tricks and Analytics with IntStreams in Java

In this episode we'll talk about IntStreams and why they're useful in Java. IntStreams can be used to replace a common looping mechanism as well as provide a very helpful utility for gathering analyt...

25 Tammi 202229min

EP55 - How to Sort with Streams in Java

EP55 - How to Sort with Streams in Java

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

24 Tammi 202237min

EP54 - What is the Map Operation in Java Streams?

EP54 - What is the Map Operation in Java Streams?

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

26 Marras 202141min

EP53 - Intro to Streams in Java

EP53 - Intro to Streams in Java

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

19 Marras 202143min

EP52 - Most Common Functional Interfaces in Java

EP52 - Most Common Functional Interfaces in Java

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

15 Marras 202153min

EP51 - Let's Talk Lambdas in Java

EP51 - Let's Talk Lambdas in Java

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

12 Marras 202157min

EP50 - #3 of 3 to Getting a Job as a Coder

EP50 - #3 of 3 to Getting a Job as a Coder

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

6 Elo 202131min

Suosittua kategoriassa Liike-elämä ja talous

sijotuskasti
mimmit-sijoittaa
rss-rahapodi
herrasmieshakkerit
ostan-asuntoja-podcast
rss-sisalto-kuntoon
psykopodiaa-podcast
rss-rahamania
inderespodi
rss-startup-ministerio
taloudellinen-mielenrauha
sijoituspodi
lakicast
rss-h-asselmoilanen
rss-lahtijat
rss-uppoava-vn-laiva
rss-myynnilla-on-asiaa-kert-kenner
sijoitusovi-podcast
bakkari-tarinoita-tapahtumien-takahuoneista
rss-seuraava-potilas