java - JPA单向一对多连接表-实体映射不起作用

标签 java hibernate jpa entity

我试图为使用以下表设计的数据库创建一些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批注

关于java - JPA单向一对多连接表-实体映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36043685/

相关文章:

java - 关于 C3P0 池化数据源的问题

java - 如何使用 postgresql 在 Spring Boot 的 addScaler() 方法中添加枚举值?

java - 空 transient 属性

java - 在这种情况下如何插入或更新数据库行

java - 使用JSP在tomcat服务器中连接到Oracle数据库时出错

hibernate - 查询以删除表中的所有行 hibernate

java - 无法映射具有多对多关系的不同实体的字段

java - JPA:删除时违反约束

java - Spring Boot 如何找出当前登录的用户?

java - 更改参数数量的Java函数调用