2

Here is Named query to fetch specific track with specific user metadata.

"SELECT t FROM Track t LEFT JOIN FETCH t.metadata tm WHERE t.id= :id AND tm.userID = :userID"

In Track entity i am having metadata list with @OneToMany relationship with join table inbetween name = tracks_metadata_join.

//TrackMetadata


 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity=TrackMetadata.class, orphanRemoval = true) 

 @JoinTable(name = "tracks_metadata_join", joinColumns = { @JoinColumn(name = "track_id", 

 referencedColumnName = "id", table = "tracks") }, inverseJoinColumns = @JoinColumn( 

 name = "track_metadata_id", referencedColumnName = "id", table = "tracks_metadata")) 

 @JsonProperty 

 private List<TrackMetadata> metadata = new ArrayList<>();

The result of above query is Track with all it's all metadata, But i want Track with particular metadata. like user specific. Thanks!

Bipin Vayalu
  • 3,025
  • 2
  • 25
  • 39

1 Answers1

0

When you read Track and initialize its metadata collection, Hibernate will populate the entire collection. That's how Hibernate works.

If you want only specific metadata elements loaded, then you could make the association bidirectional and load the desired metadata only:

select tm from TrackMetaData tm where tm.track.id = :id and tm.userID = :userID
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110