Assume this model,
@Entity
@Table(name = "play_list")
public class PlayList {
@Id @Column(name = "code")
private Long code;
...
@ManyToMany(fetch = LAZY)
@JoinTable(...)
private List<Song> songs = new ArrayList<>();
}
@Entity
@Table(name = "song")
public class Song {
@Id @Column(name = "code")
private Long code;
@Lob @Column(name = "raw_data")
private byte[] rawData; // The binary value of the song (important!)
...
@ManyToMany(mappedBy = "songs")
private List<PlayList> playLists = new ArrayList<>();
}
Then, if your execute a JPA Query like this one:
SELECT pl, s
FROM
playlist pl
INNER JOIN pl.songs s
Hibernate, executes a SQL query like this:
SELECT pl.*, s.*
FROM
play_list pl
INNER JOIN play_list_song pls ON pl.code = pls.playlist_code
INNER JOIN song s ON pls.song_code = s.code
I was debugging this code, and Hibernate does bind correctly the JDBC tuples ... let me explain with an example, if you have 2 songs and 2 playlist and both playlist contains the 2 songs, hibernate only maps 4 java objects ...
But, and this is my question, in order to create those 4 java objects, does Hibernate parse and load each JDBC tuple into memory, to later discard duplicated objects (objects of the same entity and same @Id)??
Why I'm asking this? Well, assume that you execute the previous query and you have a lot of playlists that share songs ... if hibernate loads everything before discarding duplicated objects, then it will read the byte[] rawData
a lot of times (which it could be a problem if the rawData are in the form of MB magnitude)
Thanks ...
NOTE: I'm assuming that Hibernate transform the blob data into a byte[]
using JDBC Resultset.getBinaryStream(int)
and that the database (oracle) is returning a LOB_LOCATOR
in such column ...