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)

Hibernate @ManyToMany Unidirectional and Bidirectional

Hibernate @ManyToMany Unidirectional and Bidirectional

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

22 Elo 201453min

Hibernate @OneToOne Unidirectional / Bidirectional

Hibernate @OneToOne Unidirectional / Bidirectional

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

8 Elo 201436min

Hibernate @OneToMany Bidirectional Relationship

Hibernate @OneToMany Bidirectional Relationship

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

28 Heinä 201434min

Mapping Relationships with Hibernate

Mapping Relationships with Hibernate

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

23 Heinä 201455min

Hibernate - Creating Data Access Objects (DAOs)

Hibernate - Creating Data Access Objects (DAOs)

Data Access Objects – What are they? Data Access Objects (or DAOs for short) are used as a direct line of connection and communication with our database. DAOs are used when the actual CRUD (CRUD = Cr...

15 Heinä 201459min

Hibernate's most important annotation - @Entity

Hibernate's most important annotation - @Entity

What You'll Learn The focus of this podcast / blog post is to teach you how to create the connection between your Java objects and the SQL Database tables. Remember, the whole point of the Hibernate ...

8 Heinä 201459min

Hibernate Persistence for Beginners

Hibernate Persistence for Beginners

What is a Persistence Framework? As the name implies, it has something to do with persisting things... this means that we're still talking about databases. But what is it that we are persisting with ...

3 Heinä 20141h 1min

The SQL Subquery

The SQL Subquery

The SQL Subquery Now that you've learned about SQL Joins, aggregate functionsand the group by keyword, it's time we moved on to our final topic in our SQL tutorial series. Today you'll be learning ...

25 Kesä 201448min

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