gpt4 book ai didi

java - 使用@ElementCollection、@MapKeyJoinColumn 进行多对多关系时引用关键问题

转载 作者:行者123 更新时间:2023-12-01 14:44:48 25 4
gpt4 key购买 nike

我正在尝试多对多关系,团队成员可以在多个项目上工作,一个项目可以有多个团队成员,表结构如下,

create table TBL_PROJECT_ONE(
id integer primary key generated always as identity(start with 12,increment by 3),
name varchar(50)
)

create table TBL_TEAM_MEMBER_ONE(
id integer primary key generated always as identity(start with 7,increment by 5),
name varchar(50),
salary integer
)

create table EMP_PRJ_CADRE(
MEMBER_ID integer references TBL_TEAM_MEMBER_ONE,
PRJ_ID integer references TBL_PROJECT_ONE,
CADRE varchar(10),
constraint PK_001_EMP_TEAM primary key (MEMBER_ID,PRJ_ID)
)

在这里我创建了一个新表只是为了存储关系,现在请关注 Employee 实体,

@Entity
@Table(name="TBL_TEAM_MEMBER_ONE")
public class EmployeeEntityFour implements Serializable{
public EmployeeEntityFour(){}
public EmployeeEntityFour(String empName,Integer salary){
...
..
}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer empId;

@Column(name="NAME")
private String empName;

@Column(name="SALARY")
private Integer empSal;

@ElementCollection(fetch= FetchType.LAZY)
@CollectionTable(name="EMP_PRJ_CADRE")
@MapKeyJoinColumn(name="PRJ_ID")
@Column(name="CADRE")
private Map<ProjectEntityOne,String> employeeCadre;
...
..
.
}

请遵循项目实体的映射,

@Entity
@Table(name="TBL_PROJECT_ONE")
public class ProjectEntityOne implements Serializable{
public ProjectEntityOne(){}
public ProjectEntityOne(String name){
this.projectName = name;
}

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer projectId;

@Column(name="NAME")
private String projectName;

@ElementCollection(fetch= FetchType.LAZY)
@CollectionTable(name="EMP_PRJ_CADRE")
@MapKeyJoinColumn(name="MEMBER_ID")
@Column(name="CADRE")
private Map<EmployeeEntityFour,String> employeeCadre;
....
..
.
}

在main方法测试中编写的代码如下,

ProjectEntityOne proj = new ProjectEntityOne("Citi Grand Central");        
Map<EmployeeEntityFour,String> cadreMap = new HashMap<EmployeeEntityFour,String>();
cadreMap.put(new EmployeeEntityFour("Murlinarayan Muthu",34000), "Senior Software Engineer");
cadreMap.put(new EmployeeEntityFour("Gopalkrishna Rajnathan",64000), "Software Engineer");
cadreMap.put(new EmployeeEntityFour("Premanna Swaminathan",94000), "Project Manager");

proj.setEmployeeCadre(cadreMap);

em.persist(proj);

但我收到一个错误

ERROR: 'PROJECTENTITYONE_ID' is not a column in table or VTI 'APP.EMP_PRJ_CADRE'.

当我在两个实体中都指定了 @MapKeyJoinColumn 时,我收到一个错误,因为第三个表的列不正确。

我失踪的地方

最佳答案

它以某种方式起作用了,我必须对代码进行一些更改,

首先在Entity ProjectEntityOne中编辑的代码如下,

@ElementCollection(fetch= FetchType.LAZY)
@CollectionTable(name="EMP_PRJ_CADRE",joinColumns=@JoinColumn(name="PRJ_ID"))
@MapKeyJoinColumn(name="MEMBER_ID")
@Column(name="CADRE")
private Map<EmployeeEntityFour,String> employeeCadre;

我在这里所做的是在@CollectionTable中添加了@JoinedColumn,

我在实体 EmployeeEntityFour 中所做的第二个更改,更改是我从中删除了 PorjectEntityOne 的映射,

在测试中,我可以使用员工映射保存项目,但这里所有员工都应该已经保存了。即map的key

Map<EmployeeEntityFour,String> employeeCadre;

应该已经被持久化了然后我们就可以持久化项目实体。

关于java - 使用@ElementCollection、@MapKeyJoinColumn 进行多对多关系时引用关键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547025/

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