通过@ManyToMany关联关系连接三个表

时间:2019-02-12 07:50:43

标签: java hibernate jpa spring-data-jpa jointable

通过@ManyToMany关联加入三个表

我正在使用Spring Boot和Spring Data JPA实现Book Store应用程序。我已经尝试过@onetoone和@manytoone和@onetomany关联,但是我对@ManyToMany关联感到困惑。

共有三个表格-图书,类型和作者。我想做以下 1)创建一个新表-Book_Genre_Author 2)如果数据库中已经存在Author或Genre,则不会在数据库中创建新条目。 3)如果Book已经存在于数据库中,则不会进入数据库。 4)如何使用Spring Data JPA实施插入和更新操作?

这是我的三个实体-

@Entity
@Table(name = "Book_Details")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOM_SEQUENCE")
@SequenceGenerator(sequenceName = "customer_seq", allocationSize = 1, name = "CUSTOM_SEQUENCE")
@Column(name = "BookId")
int bookId;

public int getBookId() {
    return bookId;
}

public void setBookId(int bookId) {
    this.bookId = bookId;
}

@Column(name = "BookName")
String bookName;



@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "Book_Author_Genre", joinColumns = @JoinColumn(name = "bookId"), inverseJoinColumns = @JoinColumn(name = "genreId"))
@MapKeyJoinColumn(name = "authorId")
@ElementCollection
private Map<Author, Genre> authorGenre = new HashMap<>();

public Map<Author, Genre> getAuthorGenre() {
    return authorGenre;
}

public void setAuthorGenre(Map<Author, Genre> authorGenre) {
    this.authorGenre = authorGenre;
}

@Column(name = "Language")
String language;



@Column(name = "GoodReadReviews")
String goodReadReviews;


  @Override public String toString() { return "BookId=" + this.bookId +
  "::BookName=" + this.bookName + "::AuthorId=" + "Will check how to get it" +
  "::Langauge = " + this.language + "::GenreId = " + "Will check how to get it" +
  "::GoodReadReviews=" + this.goodReadReviews;

  }


public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public String getLanguage() {
    return language;
}

public void setLanguage(String language) {
    this.language = language;
}

public String getGoodReadReviews() {
    return goodReadReviews;
}

public void setGoodReadReviews(String goodReadReviews) {
    this.goodReadReviews = goodReadReviews;
}

}

@Entity
@Table(name="Author_Details")
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="AuthorSeqGenerator")
@SequenceGenerator(sequenceName = "author_seq", allocationSize = 1, name = "AuthorSeqGenerator")
@Column(name="AuthorId")
int authorId;

@Column(name="AuthorName")
String authorName;

@Column(name="Country")
String country;

@Column(name="Gender")
String gender;


public int getAuthorId() {
    return authorId;
}
public void setAuthorId(int authorId) {
    this.authorId = authorId;
}


public String getAuthorName() {
    return authorName;
}
public void setAuthorName(String authorName) {
    this.authorName = authorName;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}

}

@Entity
@Table(name="Genre_details")
public class Genre {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="GenreSeqGenerator")
@SequenceGenerator(sequenceName = "genre_seq", allocationSize = 1, name = "GenreSeqGenerator")
int genreId;

@Column(name="GenreName")
String genreName;

public int getGenreId() {
    return genreId;
}

public void setGenreId(int genreId) {
    this.genreId = genreId;
}




public String getGenreName() {
    return genreName;
}

public void setGenreName(String genreName) {
    this.genreName = genreName;
}

}

我尝试使用以下代码保存图书详细信息-

Author author = new Author();
        author.setAuthorName("Rabindranath Tagore");
        author.setCountry("India");
        author.setGender("Male");

    Genre genre = new Genre();
    genre.setGenreName("Fiction");

    Book book = new Book();
    book.setBookName("Gitanjali");
    book.setGoodReadReviews("5");
    book.setLanguage("Bengali");
    HashMap<Author,Genre> authorGenre = new HashMap<>();
    authorGenre.put(author, genre);
    book.setAuthorGenre(authorGenre);

    bookRepository.save(book);

但是我收到以下错误消息。

原因:org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientObjectException:对象引用了未保存的瞬态实例-在刷新之前保存该瞬态实例:com.bsm.app.model.Author;嵌套异常是java.lang.IllegalStateException:org.hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例:com.bsm.app.model.Author

我天真,我的做法可能是错误的。请提出建议和支持。

1 个答案:

答案 0 :(得分:1)

我建议创建一个新的链接实体,该实体将用于映射该特定表。

您只需决定是使用复合主键还是添加人工ID(推荐)。

此设置似乎涉及更多的编码,但是根据我的经验,一旦3个表开始起作用,就会更加清楚:

class Book {
    @OneToMany(mappedBy = "book", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<BookAuthorGenreLink> bookAuthorGenreLinks;
}

class Author{
    @OneToMany(mappedBy = "author", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<BookAuthorGenreLink> bookAuthorGenreLinks;
}

class Genre{
    @OneToMany(mappedBy = "genre", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<BookAuthorGenreLink> bookAuthorGenreLinks;
}

链接表:

class BookAuthorGenreLink {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "book_id")
    private Book book;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "author_id")
    private Author author;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "genre_id")
    private Genre genre;

}