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

时间:2014-03-04 13:58:07

标签: java hibernate jpa

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

    [...]
}

1 个答案:

答案 0 :(得分:1)

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

首先,我必须更改表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;