Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define column that is not actually related to another table by keys, but by keywords
    text
    copied!<p>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.</p> <p>I have a playlist application. Each playlist has a title and a list of keywords that signify the songs to include.</p> <p>So, I may have this as one row, where there are three keywords. Exercise steady-beat_country hard_rock classic_rock</p> <p>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.</p> <p>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.</p> <p>I am curious if I should just do this in my <code>@Entity</code> model, which is written in <code>Scala</code>:</p> <pre><code>@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]() </code></pre> <p>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.</p> <p>Or, is there a way to define something like this in JPA so I don't have to make an extra call?</p> <p><strong>UPDATE:</strong></p> <p>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:</p> <p><code>SELECT Playlist p, Songs s WHERE p.user = :user AND s.keywords IN (p.keywords)</code></p> <p>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 <code>song</code> property for <code>playlist</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload