我有一个具有复合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;

09-11 20:20