使用@ ElementCollection,@ MapKeyJoinColumn做多对多关系时参考关键问题 [英] Reference key issue while doing many to many relationship using @ElementCollection, @MapKeyJoinColumn
问题描述
我正在尝试多对多关系,团队成员可以处理多个项目,一个项目可以有多个团队成员,表结构如下,
i am trying on many to many relationship, Team member can work on multiple projects and a project can have multiple team member , the table structure is as follows,
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实体,
Here i have created a new table just to store the relationship, Now please follow the Employee entity,
@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;
...
..
.
}
请遵循项目实体的映射,
Please follow the mapping for Project Entity,
@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;
....
..
.
}
在测试代码的主要方法中,如下所示,
In main method testing the code written is as follows,
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时,我也收到了错误,因为第三张表的列不正确.
When in both the entities i have specified @MapKeyJoinColumn than too i am getting an error as improper column for the third table.
我想念的地方
推荐答案
在EmployeeEntityFour中的employeeCadre上,您需要一个@JoinColumn(name ="MEMBER_ID"),并且您还需要一个@JoinColumn(name ="PRJ_ID") ProjectEntityOne雇员干部.
On employeeCadre in EmployeeEntityFour you need a @JoinColumn(name="MEMBER_ID") and you would also need a @JoinColumn(name="PRJ_ID") in the ProjectEntityOne employeeCadre.
但是,我不会这样建模.首先,您不能具有双向ElementCollection映射,并且ElementCollection只能由一侧拥有.最好的解决方案是定义一个映射到EMP_PRJ_CADRE表的Cadre实体,并从两边都具有一个OneToMany,并且每边都有一个ManyToOne.
But, I would not model it this way. First of all you cannot have a bi-directional ElementCollection mapping, and ElementCollection can only be owned by one side. The best solution would be to define an Cadre entity mapping to EMP_PRJ_CADRE table and have a OneToMany to it from both sides, and have it have a ManyToOne to each.
或者,您可以将ManyToMany与MapKeyColumn一起使用,但是我认为最好有一个实体.
Alternatively you may use a ManyToMany with a MapKeyColumn, but I think you would be better off having an entity.
这篇关于使用@ ElementCollection,@ MapKeyJoinColumn做多对多关系时参考关键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!