[英]Unable to create Mapping Entity with JPA
A. I have a entity EntityA like below: A. 我有一个实体 EntityA,如下所示:
Table - EntityA(id long PK, name varchar)
@Entity @Table
EntityA{
@Id
long id;
String name;
}
B. Based on this I want to fetch data in below class through JPA(using unidirectional mapping): B.基于此我想通过JPA(使用单向映射)获取class下面的数据:
@Entity @Table
EntityMap{
long id;
@OneToOne
EntityA entity;
@OneToMany
List<EntityA> mappedEntity;
}
C. C。 To make it work for now I have created an entity like below:为了使它现在可以工作,我创建了一个如下所示的实体:
Table - entity_map(id long pk, source_entity_id long FK-EntityA_id, target_entity_id long FK-EntityA_id)
@Entity @Table
EntityMap{
@Id
long id;
@OneToOne
@JoinColumn(name = "source_entity_id")
EntityA sourceEntity;
@ManyToOne
@JoinColumn(name = "target_entity_id")
EntityA targetEntity;
}
This is working as expected but I need entity explained in #B.这按预期工作,但我需要 #B 中解释的实体。 Any suggestion?有什么建议吗?
A @OneToMany
relationship can be archieved by @OneToMany
关系可以通过
@JoinColumn(name = "entity_map_id")
@OneToMany
List<EntityA> mappedEntity;
In your EntityA
table you need a column entity_map_id
and a foreign key (entity_map_id) references EntityMap(id)
constraint.在您的EntityA
表中,您需要一个列entity_map_id
和一个foreign key (entity_map_id) references EntityMap(id)
约束。
If you cannot change the EntityA
table you need a @JoinTable
to perform the mapping:如果您无法更改EntityA
表,则需要@JoinTable
来执行映射:
@JoinTable(name = "JOIN_TABLE", joinColumns = {@JoinColumn(name = "MY_ENTITY_MAP_FK")}, inverseJoinColumns = {@JoinColumn(name = "ENTITY_A_FK")})
@OneToMany
List<EntityA> mappedEntity;
The join table contains two columns ENTITY_A_FK
and MY_ENTITY_MAP_FK
:连接表包含两列ENTITY_A_FK
和MY_ENTITY_MAP_FK
:
create table JOIN_TABLE(
ENTITY_A_FK integer not null,
MY_ENTITY_MAP_FK integer not null
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.