gpt4 book ai didi

java - org.hibernate.AnnotationException 映射错误

转载 作者:行者123 更新时间:2023-12-02 13:29:13 24 4
gpt4 key购买 nike

我正在尝试用元素制作购物车。一个用户有多个项目,所以我这样做:我有一个 @Entity @Table 类 Item,它实现了 Serialized

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 实现 Serialized):

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"}}

最佳答案

您的注释混淆了,@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;
}

关于java - org.hibernate.AnnotationException 映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43250739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com