我正在试图制作带有物品的购物车。用户有多个项目,所以我这样做: 我有一个@Entity @Table类Item,它实现了Serializable
private User user;
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade = ALL, fetch = FetchType.EAGER, mappedBy = "items")
@Column(nullable = true, updatable = false)
public User getUser() {
return user;
}
并在User.java中(@Entity @Table实现Serializable):
private List<Item> items;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
@ManyToOne
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
我得到的是这个错误:
Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: org.auction.model.Item.user"}}
答案 0 :(得分:1)
你的注释混淆了,@ OneToMany是集合的集合
在Items类
中 @ManyToOne(cascade = ALL, fetch = FetchType.EAGER)
@Column(nullable = true, updatable = false)
public User getUser() {
return user;
}
在用户类中,您将拥有OneToMany
@OneToMany(mappedBy = "user")
public List<Item> getItems() {
return items;
}