简体   繁体   English

外键必须具有与引用的主键相同的列数。 但是我没有使用复合键

[英]Foreign key must have same number of columns as the referenced primary key. But I'm not using a composite key

I have a LibraryModel class, a LibraryImage class and a LibraryAttribute class. 我有一个LibraryModel类,一个LibraryImage类和一个LibraryAttribute类。 A LibraryModel can have an arbitrary number of LibraryImages and LibraryAttributes. LibraryModel可以具有任意数量的LibraryImages和LibraryAttributes。

The error that I get: 我得到的错误:

org.hibernate.MappingException: Foreign key (FKmbn4xh7xdxv371ao5verqueu3:library_item_attribute [LIBRARY_ITEM_ATTRIBUTE_ID])) must have same number of columns as the referenced primary key (library_item_attribute [LIBRARY_ITEM_ID,LIBRARY_ITEM_ATTRIBUTE_ID])

Here are my annotated Objects: 这是我带注释的对象:

Library Model: 库模型:

@Entity
@Table(name = "library_item", uniqueConstraints = {

})
@Inheritance(strategy = InheritanceType.JOINED)
public class LibraryItemModel implements LibraryItem{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "LIBRARY_ITEM_ID", unique = true, nullable = false)
    private Integer libraryItemId;

    @Column(name = "ITEM_TITLE", unique = false, nullable = false)
    private String itemTitle;

    @Column(name = "IS_PARENT", nullable = false)
    private Boolean isParent;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name="LIBRARY_ID", nullable = false)
    private LibraryModel libraryModel;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "ITEM_LISTING",
            joinColumns = {@JoinColumn(name = "PARENT_LIB_ITEM_ID", nullable=false)},
            inverseJoinColumns = {@JoinColumn(name="CHILD_LIB_ITEM_ID", nullable = false)})
    private Set<LibraryItemModel> itemChildren = new HashSet<>();

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "itemChildren")
    private Set<LibraryItemModel> itemParents = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "LIBRARY_ITEM_IMAGE",
            joinColumns = { @JoinColumn(name = "LIBRARY_ITEM_ID", nullable=false)},
            inverseJoinColumns = { @JoinColumn(name="LIBRARY_IMAGE_ID", nullable = false)})
    private Set<LibraryImage> itemImages = new HashSet<>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "rootLibraryItemModel")
    private Set<LibraryModel> libraries = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "LIBRARY_ITEM_ATTRIBUTE",
            joinColumns = { @JoinColumn(name = "LIBRARY_ITEM_ID", nullable =false)},
            inverseJoinColumns = { @JoinColumn(name="LIBRARY_ITEM_ATTRIBUTE_ID", nullable = false)})
    private Set<LibraryItemAttribute> libraryItemAttributes = new HashSet<>();
}

LibraryImage: LibraryImage:

@Entity
@Table(name = "library_image", uniqueConstraints = {

})
public class LibraryImage {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "LIBRARY_IMAGE_ID", unique = true, nullable = false)
    private Integer libraryImageId;

    @Column(name = "IMAGE_LOCATION")
    private String imageLocation;

    @Column(name = "IMAGE_TITLE")
    private String imageTitle;

    @Enumerated(EnumType.STRING)
    private LibraryImageType imageType;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="LIBRARY_ITEM_ID", nullable = false)
    private LibraryItemModel libraryItemModel;
    }

Library Attribute: 库属性:

@Entity
@Table(name = "library_item_attribute", uniqueConstraints = {

})
public class LibraryItemAttribute {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "LIBRARY_ITEM_ATTRIBUTE_ID", unique = true, nullable = false)
    private Integer libraryItemAttributeId;

    @Column(name = "ATTRIBUTE_NAME")
    private String attributeName;

    @Column(name = "ATTRIBUTE_VALUE")
    private String attributeValue;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="LIBRARY_ITEM_ID", nullable = false)
    private LibraryItemModel libraryItemModel;
}

This is really frustrating as the LibraryImage class is mapped without problems and doesn't throw any errors, but the LibraryAttribute class which is annotated in an identical way to the LibraryImage class is throwing this error. 这确实令人沮丧,因为LibraryImage类的映射没有问题并且不会引发任何错误,但是以与LibraryImage类相同的方式注释的LibraryAttribute类引发了此错误。

Can someone please have a look and let me know what my problem is? 有人可以看看一下,让我知道我的问题是什么吗?

Found the problem. 找到了问题。 In the LibraryItemModel class I defined the Join table with the LibraryItemAttribute to be called "LIBRARY_ITEM_ATTRIBUTE", which is the name of the table of the Library item attributes. 在LibraryItemModel类中,我将带有LibraryItemAttribute的Join表定义为“ LIBRARY_ITEM_ATTRIBUTE”,这是Library项属性表的名称。

The join table is a different table and should have a different table name. 联接表是一个不同的表,并且应该具有一个不同的表名。

For the Library Image table above, the image table is called library_image, while the join table is called LIBRARY_ITEM_IMAGE 对于上面的库映像表,映像表称为library_image,而联接表称为LIBRARY_ITEM_IMAGE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收到错误“外键必须与引用的主键具有相同的列数”,尽管未使用复合键 - Receiving Error “Foreign key must have same number of columns as the referenced primary key” despite not using composite keys 外键必须与引用的主键错误具有相同的列数,但没有具有复合键的实体 - Foreign key must have same number of columns as the referenced primary key error, but a have no entities with composite key 外键必须与引用的主键具有相同的数字列 - Foreign key must have the same number columns as the referenced primary key Hibernate MappingException:外键必须具有与引用的主键相同的列数 - Hibernate MappingException: Foreign key must have same number of columns as the referenced primary key Hibernate问题:外键必须与引用的主键具有相同数量的列 - Hibernate Issue : Foreign key must have same number of columns as referenced primary key org.hibernate.MappingException:外键XXX必须具有与引用的主键YYY相同的列数 - org.hibernate.MappingException: Foreign key XXX must have same number of columns as the referenced primary key YYY 外键必须与@ManyToMany 链接表的引用主键具有相同的列数 - Foreign key must have same number of columns as the referenced primary key of the @ManyToMany linked table org.hibernate.MappingException:外键必须具有与引用的主键相同的列数 - org.hibernate.MappingException: Foreign key must have same number of columns as the referenced primary key Hibernate 外键必须与多对多上引用的主键具有相同的列数 - Hibernate Foreign key must have same number of columns as the referenced primary key on many-to-many JPA - 字段必须与引用的主键具有相同的列数 - JPA - field must have same number of columns as the referenced primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM