我试图为使用以下表设计的数据库创建一些JPA实体:PRINCIPALS和CREDENTIALS与其他表具有以下关系:
@Entity
@Table(name = "CREDENTIALS")
public class Credentials {
@Id
@Column(name = "CREDENTIAL_ID")
private Integer credentialID;
@Id
@Column(name = "CREDENTIAL_TYPE_ID")
private String credentialTypeID;
@OneToOne
@JoinColumn(name = "CREDENTIAL_TYPE_ID", insertable = false, updatable = false)
private CredentialTypes credentialTypes;
}
CREDENTIALS与CREDENTIAL_TYPES具有oneToOne关系
@Entity
@Table(name = "CREDENTIAL_TYPES")
public class CredentialTypes {
@Id
@Column(name = "CREDENTIAL_TYPE_ID")
private String credentialTypeID;
@Column(name = "DESCRIPTION")
private String description;
}
@Entity
@Table(name = "PRINCIPALS")
public class Principals implements Serializable {
@Id
@Column(name = "PRINCIPAL_TYPE_ID", nullable = false)
private String principalTypeID;
@Column(name = "PRINCIPAL_ID", nullable = false)
private String principalID;
@OneToOne
@JoinColumn(name = "PRINCIPAL_TYPE_ID", insertable = false, updatable = false)
private PrincipalTypes principalTypes;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "PRINCIPAL_CREDENTIAL",
joinColumns = @JoinColumn(name = "CREDENTIAL_ID"),
inverseJoinColumns = @JoinColumn(name = "PRINCIPAL_ID"))
private List<Credentials> credentials;
PRINCIPALS与PRINCIPAL_TYPES有一个一对一的关系
@Entity
@Table(name = "PRINCIPAL_TYPES")
public class PrincipalTypes implements Serializable {
@Id
@Column(name = "PRINCIPAL_TYPE_ID", nullable = false)
private String principalTypeID;
@Column(name = "DESCRIPTION")
private String description;
最后,PRINCIPALS与CREDENTIALS具有oneToMany关系,并使用联接表PRINCIPLE_CREDENTIAL
@Entity
@Table(name = "PRINCIPAL_CREDENTIAL")
public class PrincipalCredential implements Serializable {
@Id
@Column(name = "PRINCIPAL_TYPE_ID", nullable = false)
private String principalTypeID;
@Id
@Column(name = "PRINCIPAL_ID", nullable = false)
private String principalID;
@Id
@Column(name = "CREDENTIAL_ID")
private Integer credentialID;
@Id
@Column(name = "CREDENTIAL_TYPE_ID")
private String credentialTypeID;
在启动时(使用SpringBoot),我收到一个关于Principals和Credentials之间的oneToMany关系的错误,只是不知道如何解决它……尝试了各种其他方法(无法更改DB设计)。
Caused by: org.hibernate.AnnotationException: A Foreign key refering entities.Principals from entities.Credentials has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:502)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1467)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1233)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:729)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:85
我发现异常,因为没有从凭证中引用原则...。
最佳答案
joinColumns / inverseJoinColumns中缺少PRINCIPLE_TYPE_ID和CREDENTIAL_TYPE_ID。我认为您必须使用@JoinColumns批注