Now using model fields to retrieve associated songs rather than querying all songs then grouping.
This commit is contained in:
parent
845284dcd4
commit
ebc4852cfc
@ -57,6 +57,7 @@ public class DatabaseManager {
|
|||||||
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
|
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
|
||||||
properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:.");
|
properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:.");
|
||||||
properties.put("hibernate.hbm2ddl.auto", "create-drop");
|
properties.put("hibernate.hbm2ddl.auto", "create-drop");
|
||||||
|
properties.put("hibernate.enable_lazy_load_no_trans", "true");
|
||||||
getInstance().sessionFactory = new Configuration()
|
getInstance().sessionFactory = new Configuration()
|
||||||
.addProperties(properties)
|
.addProperties(properties)
|
||||||
.addAnnotatedClass(Album.class)
|
.addAnnotatedClass(Album.class)
|
||||||
|
@ -7,6 +7,7 @@ import org.hibernate.Session;
|
|||||||
import org.hibernate.criterion.Restrictions;
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -70,9 +71,8 @@ public class Gateway {
|
|||||||
* @return All songs currently in the database, grouped by album.
|
* @return All songs currently in the database, grouped by album.
|
||||||
*/
|
*/
|
||||||
public static Optional<Map<Album, List<Song>>> listAllSongsGroupedByAlbum() {
|
public static Optional<Map<Album, List<Song>>> listAllSongsGroupedByAlbum() {
|
||||||
Optional<List<Song>> songList = listAllT(Song.class);
|
Optional<List<Album>> albumList = listAllT(Album.class);
|
||||||
return (songList.isPresent()) ?
|
return (albumList.isPresent()) ? Optional.of(albumList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
|
||||||
Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getAlbum)))
|
|
||||||
: Optional.empty();
|
: Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,9 +80,8 @@ public class Gateway {
|
|||||||
* @return All songs currently in the database, grouped by artist.
|
* @return All songs currently in the database, grouped by artist.
|
||||||
*/
|
*/
|
||||||
public static Optional<Map<Artist, List<Song>>> listAllSongsGroupedByArtist() {
|
public static Optional<Map<Artist, List<Song>>> listAllSongsGroupedByArtist() {
|
||||||
Optional<List<Song>> songList = listAllT(Song.class);
|
Optional<List<Artist>> artistListList = listAllT(Artist.class);
|
||||||
return (songList.isPresent()) ?
|
return (artistListList.isPresent()) ? Optional.of(artistListList.get().stream().collect(Collectors.toMap(x -> x, x -> new ArrayList<>(x.getSongs()))))
|
||||||
Optional.of(songList.get().stream().collect(Collectors.groupingBy(Song::getArtist)))
|
|
||||||
: Optional.empty();
|
: Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user