Answer the question
In order to leave comments, you need to log in
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;
}
}
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
I can be wrong, but the data may not be loaded unique, because Set means all records are unique.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question