I am using JPA to see how helpful it will be, rather than using straight SQL, and I realized today that one of my columns is incorrect.
I have a playlist application. Each playlist has a title and a list of keywords that signify the songs to include.
So, I may have this as one row, where there are three keywords. Exercise steady-beat_country hard_rock classic_rock
If I add a new song that has one of these keywords applied to it, I want it to be pulled up when I select this playlist.
I could just go through all the playlists that have this keyword and relate the new song with the playlist, but that seems to be very inefficient, if I have several users each with many playlists, if the song can be played by all the users.
I am curious if I should just do this in my @Entity
model, which is written in Scala
:
@transient
var songs : java.util.List[Song] = new java.util.ArrayList[Song]()
@OneToMany{val cascade=Array(CascadeType.ALL), val mappedBy="playlist"}
var keywords : java.util.List[Snippet] = new java.util.ArrayList[Snippet]()
Then I could just load the keywords that are actually related to the playlist and then call a named query that can load up the songs by a list of keywords.
Or, is there a way to define something like this in JPA so I don't have to make an extra call?
UPDATE:
Tonight I test this, but I am wondering if I could set up my named query to do this query, so it would be something like:
SELECT Playlist p, Songs s WHERE p.user = :user AND s.keywords IN (p.keywords)
This code is a bit flawed as the IN I need to look up how I can do it in JPA, I may need to store the keywords as comma-delimited, but I am not certain yet how I can have the songs fill in the song
property for playlist
.