Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot JPA-如何创建EntityA保存另一个表的列表ID的实体_Spring Boot_Jpa - Fatal编程技术网

Spring boot JPA-如何创建EntityA保存另一个表的列表ID的实体

Spring boot JPA-如何创建EntityA保存另一个表的列表ID的实体,spring-boot,jpa,Spring Boot,Jpa,我有一个实体A和一个实体B EntityB是一个主表 EntityA可以有实体B的多个id。因此EntityA的一列应该包含EntityB的id列表/集合 我应该能够查询EntityA,以获得EntityB的ID列表 注意:EntityA中的许多行可以引用EntityB的相同id 我在下面试过了,但我看不到这个专栏 @OneToMany(mappedBy = "todo") private List<ObjectStore> store = new ArrayLi

我有一个实体A和一个实体B

EntityB是一个主表

EntityA可以有实体B的多个id。因此EntityA的一列应该包含EntityB的id列表/集合

我应该能够查询EntityA,以获得EntityB的ID列表

注意:EntityA中的许多行可以引用EntityB的相同id

我在下面试过了,但我看不到这个专栏

@OneToMany(mappedBy = "todo")
private List<ObjectStore> store = new ArrayList<>();

@实体
公营雇员{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@NotNull(message=“名称为必填项”)
私有字符串名称;
@ElementCollection(targetClass=String.class)
@CollectionTable(name=“DEPT”,joinColumns=@JoinColumn(name=“id”))
私有列表部门=新的ArrayList(4);
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}    
}
我注意到DEPT列被添加到DEPT表中。这是不需要的


用例:员工可以持有部门列表。许多员工记录,应该能够有相同的部门引用。

您应该将表名从“dept”更改为“dept”。因为@CollectionTable将创建另一个具有给定名称的嵌入表。在您的情况下,dept已经存在,因此它将在dept中创建新列

// Employee Class
    @ElementCollection(targetClass=String.class)
    @CollectionTable(name = "DEPT_EMPLOYEE_MAPPING", joinColumns = @JoinColumn(name="Employee_id"))
    @MapKeyJoinColumn(name="Dept_Id")
    private Map<Dept,DeptEmployeeRelationData> depts;

//Dept class 
    @ElementCollection
    @CollectionTable(name="DEPT_EMPLOYEE_MAPPING",joinColumns=@JoinColumn(name="Dept_Id"))
    @MapKeyJoinColumn(name="Employee_Id")
    Map<Employee, DeptEmployeeRelationData> employees;

    @Embeddable
    class DeptemployeeRelationData {
      @Column(name="createdAt")
      DateTime createdAt;
    }
//员工类
@ElementCollection(targetClass=String.class)
@CollectionTable(name=“DEPT\u EMPLOYEE\u MAPPING”,joinColumns=@JoinColumn(name=“EMPLOYEE\u id”))
@MapKeyJoinColumn(name=“Dept\u Id”)
私人地图部门;
//系级
@元素集合
@CollectionTable(name=“DEPT\u EMPLOYEE\u MAPPING”,joinColumns=@JoinColumn(name=“DEPT\u Id”))
@MapKeyJoinColumn(name=“Employee\u Id”)
地图员工;
@可嵌入
类DeptemployeeRelationData{
@列(name=“createdAt”)
创建日期时间;
}
您的映射是多对多的。 希望这能奏效

我用过

 @ManyToMany
 @OrderColumn
 private List<String> dept = new ArrayList<String>(4);
@manytomy
@订单列
私有列表部门=新的ArrayList(4);
按照我的要求工作
DEPT,EMPLOYEE,EMPLOYEE\u DEPT created

发布您的EntityA和EntityB内容如果使用@OneToMany,会创建哪些表?@它允许重复吗?我遇到的问题是-我随机获得几个部门ID并设置为employee,如果另一个员工获得类似的部门ID-约束冲突错误
// Employee Class
    @ElementCollection(targetClass=String.class)
    @CollectionTable(name = "DEPT_EMPLOYEE_MAPPING", joinColumns = @JoinColumn(name="Employee_id"))
    @MapKeyJoinColumn(name="Dept_Id")
    private Map<Dept,DeptEmployeeRelationData> depts;

//Dept class 
    @ElementCollection
    @CollectionTable(name="DEPT_EMPLOYEE_MAPPING",joinColumns=@JoinColumn(name="Dept_Id"))
    @MapKeyJoinColumn(name="Employee_Id")
    Map<Employee, DeptEmployeeRelationData> employees;

    @Embeddable
    class DeptemployeeRelationData {
      @Column(name="createdAt")
      DateTime createdAt;
    }
 @ManyToMany
 @OrderColumn
 private List<String> dept = new ArrayList<String>(4);