JPA - 解决关系(ManyToMany + ManyToOne)

时间:2017-07-25 12:39:04

标签: java hibernate jpa

我在定义我的人际关系方面遇到了一些问题,目前我已经拥有了这些实体:

DataWarehouse

我试图在代码中建立关​​系,如下所示:

项目-DatosTabla

    @ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "datosTabla", joinColumns = @JoinColumn(name = "idProject", referencedColumnName = "idProject"), inverseJoinColumns = @JoinColumn(name = "idTable", referencedColumnName = "idTable"))
public Set<DatosTabla> getDatosTabla() {
    return datosTabla;
}

DatosTabla-项目

    @ManyToMany(mappedBy = "datosTabla")
public Set<Project> getProject() {
    return project;
}

DatosTabla-AtributosDatosTabla

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "datosTabla")
public Set<AtributosDatosTabla> getAtributoDatosTabla() {
    return atributoDatosTabla;
}

AtributosDatosTabla-DatosTabla

    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "idTable", referencedColumnName = "idTable"),
        @JoinColumn(name = "project", referencedColumnName = "project") })
public DatosTabla getDatosTabla() {
    return datosTabla;
}

当我运行应用程序时,我收到了这个错误:

引起:org.hibernate.MappingException:外键(FK40oq9lkjvyu5b4s0x9j69uop4:AtributosDatosTabla [idTable]))必须与引用的主键具有相同的列数(DatosTabla [idProject,idTable])

我尝试通过这样做在 AtributosDatosTabla 中创建复合键: 的 AtributosDatosTabla-DatosTabla

    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "idTable", referencedColumnName = "idTable"),
        @JoinColumn(name = "project", referencedColumnName = "project") })
public DatosTabla getDatosTabla() {
    return datosTabla;
}

我收到了这个错误: 引起:org.hibernate.MappingException:无法找到具有逻辑名称的列:DatosTabla中的项目

所以,我现在很困惑。有谁知道解决这个问题的方法吗?

0 个答案:

没有答案