Now using model fields to retrieve associated songs rather than querying all songs then grouping.

This commit is contained in:
neviyn 2016-03-09 04:14:01 +00:00
parent 845284dcd4
commit ebc4852cfc
2 changed files with 6 additions and 6 deletions

View File

@ -57,6 +57,7 @@ public class DatabaseManager {
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:.");
properties.put("hibernate.hbm2ddl.auto", "create-drop");
properties.put("hibernate.enable_lazy_load_no_trans", "true");
getInstance().sessionFactory = new Configuration()
.addProperties(properties)
.addAnnotatedClass(Album.class)

View File

@ -7,6 +7,7 @@ import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -70,9 +71,8 @@ public class Gateway {
* @return All songs currently in the database, grouped by album.
*/
public static Optional<Map<Album, List<Song>>> listAllSongsGroupedByAlbum() {
Optional<List<Song>> songList = listAllT(Song.class);
return (songList.isPresent()) ?
Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getAlbum)))
Optional<List<Album>> albumList = listAllT(Album.class);
return (albumList.isPresent()) ? Optional.of(albumList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
: Optional.empty();
}
@ -80,9 +80,8 @@ public class Gateway {
* @return All songs currently in the database, grouped by artist.
*/
public static Optional<Map<Artist, List<Song>>> listAllSongsGroupedByArtist() {
Optional<List<Song>> songList = listAllT(Song.class);
return (songList.isPresent()) ?
Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getArtist)))
Optional<List<Artist>> artistListList = listAllT(Artist.class);
return (artistListList.isPresent()) ? Optional.of(artistListList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
: Optional.empty();
}