2016-11-22 78 views
0

我有一個LibraryModel類,一個LibraryImage類和一個LibraryAttribute類。一個LibraryModel可以有任意數量的LibraryImages和LibraryAttributes。外鍵必須與引用的主鍵具有相同的列數。但我沒有使用複合鍵

,我得到的錯誤:

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]) 

這裏是我的標註對象:

庫模型:

@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; 
    } 

庫屬性:

@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; 
} 

爲LibraryImage類映射沒有問題,而且不會引發任何錯誤,但它在相同的方式註解到LibraryImage類LibraryAttribute類是引發此錯誤這是很無奈。

有人可以請看看,讓我知道我的問題是什麼?

回答

0

發現問題。 在LibraryItemModel類中,我使用LibraryItemAttribute定義了Join表,將其命名爲「LIBRARY_ITEM_ATTRIBUTE」,它是庫項目屬性表的名稱。

連接表是不同的表,應該有不同的表名。

對於上面的圖庫圖像表,圖像表稱爲library_image,而連接表稱爲LIBRARY_ITEM_IMAGE

相關問題