O
O
Oleg Gamega2015-12-30 23:10:13
Java
Oleg Gamega, 2015-12-30 23:10:13

How to properly set up mapping in hibernate ManyToMany?

Good day, all with the coming!
Started familiarity with spring, problems with ManyToMany

@Entity
@Table(name = "genre")
public class Genre {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;
    private Set<Book> books;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "genres")
    public Set<Book> getBooks() {
        return books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


@Entity
@Table(name = "book")
public class Book {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "book_reviews")
    private String bookReviews;
    private Set<Genre> genres;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBookReviews() {
        return bookReviews;
    }

    public void setBookReviews(String bookReviews) {
        this.bookReviews = bookReviews;
    }

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "genre_book", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
    inverseJoinColumns = @JoinColumn(name = "genre_id", referencedColumnName = "id"))
    public Set<Genre> getGenres() {
        return genres;
    }

    public void setGenres(Set<Genre> genres) {
        this.genres = genres;
    }
}


gives an error message
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: genre, for columns: [org.hibernate.mapping.Column(books)]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor, 2015-01-03
@gadfi

I can be wrong, but the data may not be loaded unique, because Set means all records are unique.

P
ppnn, 2015-05-03
@ppnn

Hibernate does not like it when annotations in front of a class field and annotations in front of a getter interfere in one class, it must be uniform. Swears at the same time on java.util.Set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question