简体   繁体   中英

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. A LibraryModel can have an arbitrary number of LibraryImages and 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:

@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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM