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

Avsnitt(112)

EP 64: CSS 101 Tutorial

EP 64: CSS 101 Tutorial

Cascading Style Sheets are a bit of a tricky beast in HTML. Given this fact, I wanted to dedicate an entire section to just this topic. Let's start with the basics, shall we? What are Cascading Styl...

17 Nov 201444min

EP 63: HTML Styles 101

EP 63: HTML Styles 101

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

4 Nov 201448min

EP 62: Introduction to HTML

EP 62: Introduction to HTML

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

29 Okt 201434min

HTML, CSS and JavaScript - Oh My!

HTML, CSS and JavaScript - Oh My!

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

20 Okt 201440min

Hibernate Group By

Hibernate Group By

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

10 Okt 201443min

Hibernate Persistence Life Cycle

Hibernate Persistence Life Cycle

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

5 Sep 201442min

Fetch Type Lazy Vs Eager

Fetch Type Lazy Vs Eager

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

27 Aug 201429min

Populärt inom Business & ekonomi

framgangspodden
badfluence
varvet
rss-jossan-nina
rss-borsens-finest
avanzapodden
rss-svart-marknad
svd-tech-brief
uppgang-och-fall
rss-dagen-med-di
fill-or-kill
rss-kort-lang-analyspodden-fran-di
rss-den-nya-ekonomin
24fragor
lastbilspodden
dynastin
rss-inga-dumma-fragor-om-pengar
rikatillsammans-om-privatekonomi-rikedom-i-livet
kapitalet-en-podd-om-ekonomi
borsmorgon