无法使用JPA创建映射实体。

huangapple 未分类评论57阅读模式
英文:

Unable to create Mapping Entity with JPA

问题

A. 我有一个类似下面的实体 EntityA:

  1. - EntityA(id 长整型 主键, name 字符串)
  2. @Entity @Table
  3. EntityA{
  4. @Id
  5. long id;
  6. String name;
  7. }

B. 基于此,我想通过 JPA 在以下类中获取数据(使用单向映射):

  1. @Entity @Table
  2. EntityMap{
  3. long id;
  4. @OneToOne
  5. EntityA entity;
  6. @OneToMany
  7. List<EntityA> mappedEntity;
  8. }

C. 为了暂时使其工作,我创建了一个类似下面的实体:

  1. - entity_map(id 长整型 主键, source_entity_id 长整型 外键-EntityA_id, target_entity_id 长整型 外键-EntityA_id)
  2. @Entity @Table
  3. EntityMap{
  4. @Id
  5. long id;
  6. @OneToOne
  7. @JoinColumn(name = "source_entity_id")
  8. EntityA sourceEntity;
  9. @ManyToOne
  10. @JoinColumn(name = "target_entity_id")
  11. EntityA targetEntity;
  12. }

这样做可以按预期工作,但我需要有关 #B 的实体的解释。有什么建议吗?

英文:

A. I have a entity EntityA like below:

  1. Table - EntityA(id long PK, name varchar)
  2. @Entity @Table
  3. EntityA{
  4. @Id
  5. long id;
  6. String name;
  7. }

B. Based on this I want to fetch data in below class through JPA(using unidirectional mapping):

  1. @Entity @Table
  2. EntityMap{
  3. long id;
  4. @OneToOne
  5. EntityA entity;
  6. @OneToMany
  7. List&lt;EntityA&gt; mappedEntity;
  8. }

C. To make it work for now I have created an entity like below:

  1. Table - entity_map(id long pk, source_entity_id long FK-EntityA_id, target_entity_id long FK-EntityA_id)
  2. @Entity @Table
  3. EntityMap{
  4. @Id
  5. long id;
  6. @OneToOne
  7. @JoinColumn(name = &quot;source_entity_id&quot;)
  8. EntityA sourceEntity;
  9. @ManyToOne
  10. @JoinColumn(name = &quot;target_entity_id&quot;)
  11. EntityA targetEntity;
  12. }

This is working as expected but I need entity explained in #B. Any suggestion?

答案1

得分: 0

一个 @OneToMany 的关联可以通过以下方式实现:

  1. @JoinColumn(name = "entity_map_id")
  2. @OneToMany
  3. List<EntityA> mappedEntity;

在你的 EntityA 表中,你需要一个名为 entity_map_id 的列,并且有一个 foreign key (entity_map_id) references EntityMap(id) 的约束。

如果你不能改变 EntityA 表,你需要使用 @JoinTable 来执行映射:

  1. @JoinTable(name = "JOIN_TABLE", joinColumns = {@JoinColumn(name = "MY_ENTITY_MAP_FK")}, inverseJoinColumns = {@JoinColumn(name = "ENTITY_A_FK")})
  2. @OneToMany
  3. List<EntityA> mappedEntity;

这个关联表包含两个列:ENTITY_A_FKMY_ENTITY_MAP_FK

  1. create table JOIN_TABLE(
  2. ENTITY_A_FK integer not null,
  3. MY_ENTITY_MAP_FK integer not null
  4. );
英文:

A @OneToMany relationship can be archieved by

  1. @JoinColumn(name = &quot;entity_map_id&quot;)
  2. @OneToMany
  3. List&lt;EntityA&gt; mappedEntity;

In your EntityA table you need a column entity_map_id and a foreign key (entity_map_id) references EntityMap(id) constraint.

If you cannot change the EntityA table you need a @JoinTable to perform the mapping:

  1. @JoinTable(name = &quot;JOIN_TABLE&quot;, joinColumns = {@JoinColumn(name = &quot;MY_ENTITY_MAP_FK&quot;)}, inverseJoinColumns = {@JoinColumn(name = &quot;ENTITY_A_FK&quot;)})
  2. @OneToMany
  3. List&lt;EntityA&gt; mappedEntity;

The join table contains two columns ENTITY_A_FK and MY_ENTITY_MAP_FK:

  1. create table JOIN_TABLE(
  2. ENTITY_A_FK integer not null,
  3. MY_ENTITY_MAP_FK integer not null
  4. );

huangapple
  • 本文由 发表于 2020年4月8日 14:29:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/61094556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定