Reputation: 823
I have tried to create some JPA Entities for a DB designed with the following tables: PRINCIPALS and CREDENTIALS which have the following relations with other tables:
@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 has a oneToOne relation with CREDENTIAL_TYPES
@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 has a oneToOne relation with 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;
And finally PRINCIPALS has a oneToMany relation with CREDENTIALS and uses a join table 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;
At startup (using SpringBoot) I receive an error for the oneToMany relation between Principals and Credentials and just don't have any idea how to fix it... Tried various other methods (The DB design cannot be changed).
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
I find the exception wierd because there is no refering of Principlas from Credentials....
Upvotes: 0
Views: 1272
Reputation: 4914
PRINCIPLE_TYPE_ID and CREDENTIAL_TYPE_ID are missing in the joinColumns/inverseJoinColumns. I think you must use the @JoinColumns Annotation
Upvotes: 1