如何管理具有复合 ID 的实体中的枚举集合



>我有一个具有复合 ID 和枚举集合的实体,但我无法设置 JPA 注释以正确配置它。

这里是表的 SQL:

create table `ReadWriteRight` (
    `idProfil` bigint not null,
    `idState` bigint not null,
    `read` boolean,
    `write` boolean,
    primary key (`idProfil`, `idState`),
    constraint `FK_ReadWriteRight_Profil` foreign key(`idProfil`) REFERENCES `Profil`(`idProfil`),
    constraint `FK_ReadWriteRight_State` foreign key(`idState`) REFERENCES `State`(`idState`)
) engine=InnoDB default charset=utf8;
create table `AssoReadRight_Form` (
    `idProfil` bigint not null,
    `idState` bigint not null,
    `typeForm` varchar(50) not null,
    primary key (`idProfil`, `idState`, `typeForm`),
    constraint `FK_AssoReadRight_Form_Profil` foreign key(`idProfil`) REFERENCES `Profil`(`idProfil`),
    constraint `FK_AssoReadRight_Form_State` foreign key(`idState`) REFERENCES `State`(`idState`)
) engine=InnoDB default charset=utf8;
create table `AssoWriteRight_Form` (
    `idProfil` bigint not null,
    `idState` bigint` not null,
    `typeForm` varchar(50) not null,
    primary key (`idProfil`, `idState`, `typeForm`),
    constraint `FK_AssoWriteRight_Form_Profil` foreign key(`idProfil`) REFERENCES `Profil`(`idProfil`),
    constraint `FK_AssoWriteRight_Form_State` foreign key(`idState`) REFERENCES `State`(`idState`)
) engine=InnoDB default charset=utf8;

这里有带有JPA注释的Java:

@Entity
@Table(name = "ReadWriteRight")
public class ReadWriteRight implements Serializable {
    private static final long serialVersionUID = 1L;
    public enum TypeForm {
        Form1, Form2;
    }
    @Embeddable
    public static final class ReadWriteRightId implements Serializable {
        private static final long serialVersionUID = 1L;
        @ManyToOne
        @JoinColumn(name = "idProfil", nullable = false)
        private Profil profil;
        @ManyToOne
        @JoinColumn(name = "idState", nullable = false)
        private State state;
        [...]
    }
    @EmbeddedId
    private ReadWriteRightId id;
    @Column(name = "read")
    private boolean read;
    @Column(name = "write")
    private boolean write;
    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = TypeForm.class, fetch = FetchType.LAZY)
    @CollectionTable(name = "AssoReadRight_Form", joinColumns = {@JoinColumn(name = "idProfil", nullable = false), @JoinColumn(name = "idState", nullable = false)})
    @Column(name = "typeForm")
    private Set<TypeForm> formulairesLecture;
    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = TypeForm.class, fetch = FetchType.LAZY)
    @CollectionTable(name = "AssoWriteRight_Form", joinColumns = {@JoinColumn(name = "idProfil", nullable = false), @JoinColumn(name = "idState", nullable = false)})
    @Column(name = "typeForm")
    private Set<TypeForm> formulairesEcriture;
    [...]
}

我已经通过一些修改解决了我的问题:

首先,我必须更改表的外键AssoWriteRight_FormAssoReadRight_Form,以便它们链接到表ReadWriteRight中的复合 id:

create table `AssoReadRight_Form` (
    `idProfil` bigint not null,
    `idState` bigint not null,
    `typeForm` varchar(50) not null,
    primary key (`idProfil`, `idState`, `typeForm`),
    constraint `FK_AssoReadRight_Form` foreign key(`idProfil`, `idState`) REFERENCES `ReadWriteRight`(`idProfil`, `idState`)
) engine=InnoDB default charset=utf8;
create table `AssoWriteRight_Form` (
    `idProfil` bigint not null,
    `idState` bigint` not null,
    `typeForm` varchar(50) not null,
    primary key (`idProfil`, `idState`, `typeForm`),
    constraint `FK_AssoWriteRight_Form` foreign key(`idProfil`, `idState`) REFERENCES `ReadWriteRight`(`idProfil`, `idState`)
) engine=InnoDB default charset=utf8;

我还必须更改我的 JPA 配置,以便关联表和基表之间的@JoinColumn链接正确:

@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = TypeForm.class, fetch = FetchType.LAZY)
@CollectionTable(name = "AssoReadRight_Form", joinColumns = {@JoinColumn(name = "idProfil", nullable = false, referencedColumnName = "idProfil"),
        @JoinColumn(name = "idState", nullable = false, referencedColumnName = "idState")})
@Column(name = "typeForm")
private Set<TypeForm> formulairesLecture;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = TypeForm.class, fetch = FetchType.LAZY)
@CollectionTable(name = "AssoWriteRight_Form", joinColumns = {@JoinColumn(name = "idProfil", nullable = false, referencedColumnName = "idProfil"),
        @JoinColumn(name = "idState", nullable = false, referencedColumnName = "idState")})
@Column(name = "typeForm")
private Set<TypeForm> formulairesEcriture;

最新更新