Spring jpa两个表共享密钥,即forigen密钥和复合密钥的一部分

Spring jpa两个表共享密钥,即forigen密钥和复合密钥的一部分,spring,hibernate,jpa,Spring,Hibernate,Jpa,我有两张桌子 CREATE TABLE public.account ( id uuid NOT NULL, "name" varchar(255) NULL, CONSTRAINT account_pkey PRIMARY KEY (id) ); CREATE TABLE public.account_ext ( id uuid NOT NULL, external_account_typ

我有两张桌子

    CREATE TABLE public.account (
        id uuid NOT NULL,
        "name" varchar(255) NULL,
        CONSTRAINT account_pkey PRIMARY KEY (id)
    );

    CREATE TABLE public.account_ext (
        id uuid NOT NULL,
        external_account_type varchar(255) NOT NULL,
        external_account_id varchar(255) NOT NULL,
        account_entity_id uuid NULL,
        CONSTRAINT account_ext_pkey PRIMARY KEY (id, external_account_type, external_account_id)
        FOREIGN KEY (id) REFERENCES account (id)
    );
我想使用JPA来映射它们 要求

  • 当使用account\u ext创建帐户时,所有account ext将获得帐户i=自动递增id 2.account_ext中3个字段的组合将是唯一的约束
  • 这就是我一直试图做但没有成功的事情

    @Entity
    @Data
    @Table(name = "account")
    @EqualsAndHashCode
    public class AccountEntity extends BaseEntity{
    
        @Column
        private String name;
    
        @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "accountEntity")
        private Set<ExternalAccountEntity> externalAccounts = new HashSet<>();
    } 
    
        @Entity
        @Data
        @Accessors(chain = true)
        @Table(name = "account_ext")
        public class ExternalAccountEntity {
    
          @EmbeddedId private ExternalAccountEntityId externalAccountEntityId;
    
          @ToString.Exclude
          @EqualsAndHashCode.Exclude
          @ManyToOne(fetch = FetchType.LAZY)
          private AccountEntity accountEntity;
        }
    
            @Data
            @Builder
            @AllArgsConstructor
            @NoArgsConstructor
            @Embeddable
        public class ExternalAccountEntityId implements Serializable {
    
          private static final long serialVersionUID = -8173857210615808268L;
    
          @Column private UUID id;
          @Column private String externalAccountId;
          @Enumerated(EnumType.STRING)
          private ExternalAccountType externalAccountType;
        }
    
    @实体
    @资料
    @表(name=“account”)
    @EqualsAndHashCode
    公共类AccountEntity扩展了BaseEntity{
    @纵队
    私有字符串名称;
    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,orphanRemoving=true,mappedBy=“accountEntity”)
    private Set externalAccounts=new HashSet();
    } 
    @实体
    @资料
    @访问器(链=真)
    @表(name=“account\u ext”)
    公共类ExternalAccountEntity{
    @EmbeddedId私有ExternalAccountEntityId ExternalAccountEntityId;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @manytone(fetch=FetchType.LAZY)
    私人会计实体;
    }
    @资料
    @建筑商
    @AllArgsConstructor
    @诺尔格构装师
    @可嵌入
    公共类ExternalAccountEntityId实现可序列化{
    私有静态最终长serialVersionUID=-8173857210615808268L;
    @列专用UUID id;
    @列私有字符串externalAccountId;
    @枚举(EnumType.STRING)
    私有ExternalAccountType ExternalAccountType;
    }
    
    有人能帮我吗