Home Ask Login Register

Developers Planet

Your answer is one click away!

Bipin Vayalu February 2016

Not getting specific data from detail table

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.


 @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")) 


 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!


Dragan Bozanovic February 2016

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

Post Status

Asked in February 2016
Viewed 1,431 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life