Newer
Older
simple-opds / src / main / java / ru / mcs / sopds / entity / Genre.java
@malexple malexple on 10 Oct 969 bytes add templates
package ru.mcs.sopds.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@Table(name = "genres")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Genre {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @EqualsAndHashCode.Include
    private Long id;

    @Column(name = "genre", nullable = false)
    private String genre;

    @Column(name = "section")
    private String section;

    @Column(name = "subsection")
    private String subsection;

    @ManyToMany(mappedBy = "genres")
    @ToString.Exclude
    private Set<Book> books = new HashSet<>();

    // Transient field for book count
    @Transient
    private Long bookCount;

    public Long getBookCount() {
        if (bookCount != null) {
            return bookCount;
        }
        return books != null ? (long) books.size() : 0L;
    }
}