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;
[...]
}