Java Spring引导中的多个映射问题
我正在尝试在两个表用户和产品之间进行多对多映射。我编写了它们的实体和存储库,但应用程序仍然给出了错误。如果可以的话,请帮助我,提前谢谢 错误Java Spring引导中的多个映射问题,java,database,hibernate,spring-boot,Java,Database,Hibernate,Spring Boot,我正在尝试在两个表用户和产品之间进行多对多映射。我编写了它们的实体和存储库,但应用程序仍然给出了错误。如果可以的话,请帮助我,提前谢谢 错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigur
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.poc.joins.entities.User.users in com.poc.joins.entities.Product.users
package com.poc.joins.entities;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String password;
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "users_products",
joinColumns = {@JoinColumn(name = "username")},
inverseJoinColumns = {@JoinColumn(name = "id")})
private Set<Product> products = new HashSet<>();
}
// Getter, setters, constructors are not shown here
代码片段是
用户
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.poc.joins.entities.User.users in com.poc.joins.entities.Product.users
package com.poc.joins.entities;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String password;
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "users_products",
joinColumns = {@JoinColumn(name = "username")},
inverseJoinColumns = {@JoinColumn(name = "id")})
private Set<Product> products = new HashSet<>();
}
// Getter, setters, constructors are not shown here
package com.poc.joins.entities;
导入javax.persistence.*;
导入java.util.HashSet;
导入java.util.Set;
@实体
@表(name=“users”)
公共类用户{
@身份证
私有字符串用户名;
私有字符串密码;
@多个(级联=级联类型.MERGE)
@JoinTable(name=“用户\产品”,
joinColumns={@JoinColumn(name=“username”)},
inverseJoinColumns={@JoinColumn(name=“id”)}
私有集合产品=新HashSet();
}
//这里不显示Getter、setter和构造函数
产品
package com.poc.joins.entities;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String p_name;
private Integer quantity;
private Float price;
private Float total;
@ManyToMany(mappedBy = "users")
private Set<User> users = new HashSet< >();
}
// Getter, setters, constructors are not shown here
package com.poc.joins.entities;
导入javax.persistence.*;
导入java.util.HashSet;
导入java.util.Set;
@实体
@表(name=“产品”)
公共类产品{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有整数id;
私有字符串p_名称;
私有整数数量;
私人浮动价格;
私人浮动总额;
@多人(mappedBy=“用户”)
私有集用户=新HashSet<>();
}
//这里不显示Getter、setter和构造函数
在拥有的实体(产品
)中,您传入拥有关系的字段(在用户
实体中):
@ManyToMany(mappedBy=“products”)
私有集用户=新HashSet<>();
最初,您告诉持久性提供程序在
用户
实体中查找名为用户
的字段,该字段将保存有关关系的所有信息(@JoinTable等),该字段非常有效!非常感谢。如果你有空余时间,你能解释一下为什么我们需要做这样的改变吗?