简体   繁体   English

jpa一对多复合主键映射

[英]jpa one to many composite primary key mapping

I have 2 tables as follows : 我有2个表如下:

TABLE_A
-------
A_SIREN
A_NDA

TABLE_B
-------
B_id    
B_NDA
B_SIREN

The id of table A is a COMPOSITE KEY SIREN/NDA 表A的id是COMPOSITE KEY SIREN / NDA

Here's the entities code. 这是实体代码。

Key class 关键课程

@Embeddable
public class SirenNdaKey implements Serializable {
  @Column(name = "A_SIREN")        
  protected String siren;
  @Column(name = "A_NDA")        
  protected String nda;
  // getters setters
}

TABLE A 表A.

public class EntityA{

   @EmbeddedId  
   private SirenNdaKey sirenNda;

   @OneToMany(fetch = FetchType.LAZY)
      @PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "A_SIREN", 
    referencedColumnName = "B_SIREN"),
      @PrimaryKeyJoinColumn(name = "A_NDA", referencedColumnName = "B_NDA") 
    })
   private Set<EntityB> EntityBSet;
   ...
}

TABLE B 表B

public class EntityB
    @Id
    private long id;
    @Column(name = "B_SIREN")
    private String siren;    
    @Column(name = "B_NDA")
    private String nda;
}

Hibernate generate 3 tables : TABLE_A, TABLE_B and association table that contain A_NDA, A_SIREN, B_ID. Hibernate生成3个表:TABLE_A,TABLE_B和包含A_NDA,A_SIREN,B_ID的关联表。 How can I do to generate only 2 tables. 如何只生成2个表。

My objectif is to find a list of entityB associated to the same couple SIREN/NDA from entity A ( entityA.getEntityBSet() ) without the need of the association table, because my tables are supplied by external batch. 我的目标是在不需要关联表的情况下entityA.getEntityBSet()与实体A( entityA.getEntityBSet() )相同的一对SIREN / NDA关联的entityB列表,因为我的表由外部批处理提供。

entityA = entityARepository.findOne(new SirenNdaKey("nda_test1", "siren_test1"));
entityA.getEntityBSet()  // this list is always empty

This is the correct source code, I should use @JoinColumns instead of @PrimaryKeyJoinColumns 这是正确的源代码,我应该使用@JoinColumns而不是@PrimaryKeyJoinColumns

public class EntityA{

   @EmbeddedId  
   private SirenNdaKey sirenNda;

   @OneToMany(fetch = FetchType.LAZY)
      @JoinColumns({ @JoinColumn(name = "B_SIREN", 
    referencedColumnName = "A_SIREN"),
      @JoinColumn(name = "B_NDA", referencedColumnName = "A_NDA") 
    })
   private Set<EntityB> EntityBSet;
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM