我有复合ID的实体
@Entity
@IdClass(value = BorrowId.class)
@Table(name = "BORROW")
public class Borrow {
@Id
@Column(name = "BOOK_ID", insertable = false, updatable = false)
private long bookId;
@Id
@Column(name = "BORROWER_ID", insertable = false, updatable = false)
private long borrowerId;
@Id
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "BOOK_ID")
private Book book;
@Id
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "BORROWER_ID")
private Borrower borrower;
@Id
@Column(name = "BORROW_DATE")
private Date borrowDate;
@Column(name = "RETURN_DATE")
private Date returnDate;
BorrowId有属性(所有东西都有getter / setter)
private long bookId;
private long borrowerId;
private Date borrowDate;
和equals,hashCode方法
当我试图坚持Borrow实体(书籍和借阅者属性设置为相应的实体,然后调用entityManager.persist(借用);)
我有我的日志:
Hibernate: insert into BORROW (BORROWER_ID, BOOK_ID, RETURN_DATE,
BORROW_DATE) values (?, ?, ?, ?) // this insert statement is correct
(that’s a look of my table) Invalid value “5” for parameter
“parameterIndex” [90008-174]
所以看起来有人做错了什么:)
怎么解决这个?我的实体声明有问题吗? (我想用CompositeId来学习一些关于使用它的东西)
最佳答案 感谢@JB nizet
@Id
@Column(name = "BOOK_ID", insertable = false, updatable = false)
private long bookId;
@Id
@Column(name = "BORROWER_ID", insertable = false, updatable = false)
private long borrowerId;
@MapsId("bookId")
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.REFRESH})
@JoinColumn(name = "BOOK_ID")
private Book book;
@MapsId("borrowerId")
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.REFRESH})
@JoinColumn(name = "BORROWER_ID")
private Borrower borrower;
@Id
@Column(name = "BORROW_DATE")
private Date borrowDate;
解决这个问题,很奇怪,因为在任何示例/教程中都没有@IdClass使用@MapsId.
我不确定现在是否需要@JoinColumn