[BAEL-4056] Guide to MultipleBagFetchException (#10294)

* Adds Domain Models
* Adds Integration Tests
This commit is contained in:
Emmanuel Yasa
2020-12-01 15:22:46 +08:00
committed by GitHub
parent 06bebff4cb
commit cd6f187e0a
10 changed files with 572 additions and 0 deletions
@@ -0,0 +1,52 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
class Album {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "album")
private List<Song> songs;
@ManyToMany(mappedBy = "followingAlbums")
private Set<User> followers;
Album(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Album album = (Album) o;
return Objects.equals(id, album.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Album() {
}
}
@@ -0,0 +1,56 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
class Artist {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "artist")
private List<Song> songs;
@OneToMany(mappedBy = "artist", cascade = CascadeType.PERSIST)
private List<Offer> offers;
Artist(String name) {
this.name = name;
this.offers = new ArrayList<>();
}
void createOffer(String description) {
this.offers.add(new Offer(description, this));
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Artist artist = (Artist) o;
return Objects.equals(id, artist.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Artist() {
}
}
@@ -0,0 +1,43 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.List;
import java.util.Objects;
@Entity
class DummyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ElementCollection
private List<String> collection1;
@ElementCollection
private List<String> collection2;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
DummyEntity that = (DummyEntity) o;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected DummyEntity() {
}
}
@@ -0,0 +1,52 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.util.Objects;
@Entity
class FavoriteSong {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Song song;
@ManyToOne
private User user;
@Column(name = "arrangement_index", nullable = false)
private int arrangementIndex;
FavoriteSong(Song song, User user) {
this.song = song;
this.user = user;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
FavoriteSong likedSong = (FavoriteSong) o;
return Objects.equals(id, likedSong.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected FavoriteSong() {
}
}
@@ -0,0 +1,46 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
class Offer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private Artist artist;
Offer(String name, Artist artist) {
this.name = name;
this.artist = artist;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Offer offer = (Offer) o;
return id != null ? id.equals(offer.id) : offer.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Offer() {
}
}
@@ -0,0 +1,47 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.util.Objects;
@Entity
class Playlist {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private User createdBy;
Playlist(String name, User createdBy) {
this.name = name;
this.createdBy = createdBy;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Playlist playlist = (Playlist) o;
return Objects.equals(id, playlist.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Playlist() {
}
}
@@ -0,0 +1,55 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.util.Objects;
@Entity
class Song {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private Album album;
@ManyToOne
private Artist artist;
Song(String name, Artist artist) {
this.name = name;
this.artist = artist;
}
void assignToAlbum(Album album) {
this.album = album;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Song song = (Song) o;
return Objects.equals(id, song.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected Song() {
}
}
@@ -0,0 +1,74 @@
package com.baeldung.jpa.multiplebagfetchexception;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "createdBy", cascade = CascadeType.PERSIST)
private List<Playlist> playlists;
@OneToMany(mappedBy = "user", cascade = CascadeType.PERSIST)
@OrderColumn(name = "arrangement_index")
private List<FavoriteSong> favoriteSongs;
@ManyToMany
private Set<Album> followingAlbums;
User(String name) {
this.name = name;
this.playlists = new ArrayList<>();
this.favoriteSongs = new ArrayList<>();
this.followingAlbums = new HashSet<>();
}
void followAlbum(Album album) {
this.followingAlbums.add(album);
}
void createPlaylist(String name) {
this.playlists.add(new Playlist(name, this));
}
void addSongToFavorites(Song song) {
this.favoriteSongs.add(new FavoriteSong(song, this));
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected User() {
}
}