gpt4 book ai didi

java - JPA多对多问题

转载 作者:行者123 更新时间:2023-12-01 05:19:06 24 4
gpt4 key购买 nike

我有两个表:用户和产品。我试图自动创建一个新的表 TestedProducts,这是用户和产品之间的多对多关系。但是,我收到此错误。

org.apache.jasper.JasperException: javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
<openjpa-1.2.1-r752877:753278 fatal user error> org.apache.openjpa.persistence.ArgumentException:
"ejb.UserEntity.products<element:interface ejb.Product>" has columns with targets, but OpenJPA does not support any joins on this mapping in this context.

UserEntity 看起来很简单。

long id;

String firstname;
String lastname;
String password;
String email;
String description;

List<Product> products;

public void setId(long id){
this.id = id;
}

@Id
@GeneratedValue
public long getId() {
return id;
}

@ManyToMany
@JoinTable(name="TestedProducts",
joinColumns=
@JoinColumn(name="usrId", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="prodId", referencedColumnName="id")
)
public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

ProductEntity 是这样的:

long id;

String name;
String description;
String type;

List<User> users;

@ManyToMany(mappedBy="products")
protected List<User> getUsers() {
return users;
}

protected void setUsers(List<User> users) {
this.users = users;
}

我跳过了其余的 setter 和 getter。

最佳答案

您需要以与在用户实体中相同的方式注释产品实体中包含用户的列表,只是相反。我认为mappedby 值的处理方式不同。

关于java - JPA多对多问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10700683/

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