2014-03-04 25 views
0

我有一個具有複合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; 

而這裏的Java與JPA註解:

@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

我已經解決了我的問題,有一些修改:

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

我也不得不改變我的JP​​A配置,以便關聯表和基表之間的正確鏈接@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;