Fixing Duplicate Data from Hibernate Queries episode artwork

EPISODE · Sep 15, 2014 · 31 MIN

Fixing Duplicate Data from Hibernate Queries

from How to Program with Java Podcast · host Trevor Page: Java Guru | Programmer | Teacher

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: Declare your joining object as a Set Make use of Distinct Root Entity Results Transformer Click Here to Keep Reading

Episode metadata supplied by the publisher feed · Published Sep 15, 2014

Embed this episode

NOW PLAYING

Fixing Duplicate Data from Hibernate Queries

0:00 31:48

No transcript for this episode yet

We transcribe on demand. Request one and we'll notify you when it's ready — usually under 10 minutes.

No similar podcasts found.

Frequently Asked Questions

How long is this episode of How to Program with Java Podcast?

This episode is 31 minutes long.

When was this How to Program with Java Podcast episode published?

This episode was published on September 15, 2014.

Can I download this How to Program with Java Podcast episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.
URL copied to clipboard!