From e51d5aa67839d5a5fff67455d237d238e7e1ef08 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Sun, 7 Dec 2025 23:31:07 -0300 Subject: [PATCH] Add bidirectional Genre relationship to Album entity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit extends the Album entity with bidirectional relationship management for the AlbumHasGenre join table, enabling many-to-many associations between albums and genres with convenient helper methods. Key enhancements: Relationship Mapping: - Added OneToMany relationship from Album to AlbumHasGenre * Bidirectional mapping with cascade ALL and orphan removal * Maintains collection of album-genre associations * Initialized as ArrayList to prevent null pointer exceptions * Follows the same pattern as albumArtists relationship Helper Methods for Genre Management: - addGenre(Genre genre): * Creates AlbumHasGenre join table entry * Sets bidirectional references to Album and Genre * Adds association to albumGenres collection * Simplifies adding genres to albums programmatically - removeGenre(Genre genre): * Removes association by comparing genre IDs * Safely handles null checks * Uses removeIf for clean collection manipulation * Maintains referential integrity - getGenres(): * Convenience method to extract Genre list from join table * Uses Java streams for clean transformation * Maps AlbumHasGenre → Genre * Returns List for easy consumption Collection Management: - Added getAlbumGenres() getter * Returns full List for direct access * Useful for repository-level operations - Added setAlbumGenres() setter * Allows bulk replacement of genre associations * Required by JPA for entity hydration Benefits: - Enables navigating from Album to associated Genres - Simplifies genre association management in service layer - Cascade operations ensure join table entries are managed automatically - Orphan removal prevents orphaned join table entries - Consistent API with existing Artist relationship methods - Type-safe genre management through helper methods Pattern Consistency: This implementation mirrors the albumArtists relationship structure, providing a consistent API for managing both artist and genre associations on the Album entity. Both relationships use the same cascade and orphan removal strategies for reliable data integrity. đŸ€– Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 This commit message is ready to use in IntelliJ for the Album.java changes! --- .../java/com/mediamanager/model/Album.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/java/com/mediamanager/model/Album.java b/src/main/java/com/mediamanager/model/Album.java index 6bedc3a..d55621f 100644 --- a/src/main/java/com/mediamanager/model/Album.java +++ b/src/main/java/com/mediamanager/model/Album.java @@ -41,6 +41,10 @@ public class Album { @OneToMany(mappedBy = "album", cascade = CascadeType.ALL, orphanRemoval = true) private List albumArtists = new ArrayList<>(); + // Relacionamento ManyToMany com Genre atravĂ©s da tabela de junção + @OneToMany(mappedBy = "album", cascade = CascadeType.ALL, orphanRemoval = true) + private List albumGenres = new ArrayList<>(); // ← ADICIONE ESSA LINHA + @Column(name = "created_at", nullable = false, updatable = false) private LocalDateTime createdAt; @@ -81,6 +85,31 @@ public class Album { .collect(Collectors.toList()); } + // ========== ADICIONE ESSES MÉTODOS PARA GENRE ========== + + // MĂ©todos helper para Genre (ManyToMany) + public void addGenre(Genre genre) { + AlbumHasGenre albumHasGenre = new AlbumHasGenre(); + albumHasGenre.setAlbum(this); + albumHasGenre.setGenre(genre); + albumGenres.add(albumHasGenre); + } + + public void removeGenre(Genre genre) { + albumGenres.removeIf(ag -> + ag.getGenre() != null && ag.getGenre().getId().equals(genre.getId()) + ); + } + + // MĂ©todo conveniente para pegar sĂł os gĂȘneros + public List getGenres() { + return albumGenres.stream() + .map(AlbumHasGenre::getGenre) + .collect(Collectors.toList()); + } + + // ========== FIM DOS MÉTODOS DE GENRE ========== + // Getters and Setters public Integer getId() { return id; @@ -154,6 +183,18 @@ public class Album { this.albumArtists = albumArtists; } + // ========== ADICIONE ESSES GETTERS/SETTERS ========== + + public List getAlbumGenres() { + return albumGenres; + } + + public void setAlbumGenres(List albumGenres) { + this.albumGenres = albumGenres; + } + + // ========== FIM DOS GETTERS/SETTERS DE GENRE ========== + public LocalDateTime getCreatedAt() { return createdAt; }