无法映射ManyToOne冲突

时间:2018-11-19 14:51:35

标签: hibernate jpa

我有Book User和Basket课。

1个用户可以在购物篮中拥有一本或多本图书。

我想映射该关系,但我不断收到以下错误:

javax.persistence.PersistenceException:[PersistenceUnit:默认]无法构建Hibernate SessionFactory;无法创建Hibernate SessionFactory。嵌套的异常是org.hibernate.persister.walking.spi.WalkingException:已经访问了关联:AssociationKey(table = basket, 列= {book_id})

购物篮

@Entity
public class Basket {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="book_id")
private Book book;
private long user_id;


public Basket() {}
public Basket(Book book, long user_id) {
    this.book = book;
    this.user_id = user_id;
}


// Getters and Setters
}

图书班

@Entity
@Table(name = "book")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Book implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

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

@Column(name = "quantity")
private Integer quantity;

@Column(name = "price")
private Double price;

@OneToMany(mappedBy = "book", fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONE)
@JsonIgnoreProperties("book")
private Set<SaleDetails> saleDetails = new HashSet<>();

@ManyToMany(fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "book_author",
           joinColumns = @JoinColumn(name = "books_id", referencedColumnName = "id"),
           inverseJoinColumns = @JoinColumn(name = "authors_id", referencedColumnName = "id"))
@JsonIgnoreProperties("books")
private Set<Author> authors = new HashSet<>();

@ManyToMany(fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "book_genre",
           joinColumns = @JoinColumn(name = "books_id", referencedColumnName = "id"),
           inverseJoinColumns = @JoinColumn(name = "genres_id", referencedColumnName = "id"))
private Set<Genre> genres = new HashSet<>();

0 个答案:

没有答案