org.hibernate.AnnotationException:从y引用x的外键具有错误的列数。应该是n

时间:2014-08-26 07:46:31

标签: java hibernate jpa

在hibernate初始化期间发生以下异常:

org.hibernate.AnnotationException: A Foreign key refering com.maar.projectbuilder.domain.Language from com.maar.projectbuilder.domain.ItemInfo has the wrong number of column. should be 0

I have the tables as in the screenshot above

我有如上面屏幕截图中的表格。 实体类编写如下。

public class ItemInfo implements Serializable {

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

   @Id
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "item_id", nullable = false)
   private Item itemId;

   @ManyToOne(targetEntity = Language.class,cascade = CascadeType.ALL)
   @JoinColumn(name = "language_item_id", nullable = false)
   private Language languageItemId;

   @Column(name = "type", nullable = false)
   private Integer type;
   //getters,setters,constructors
}
另一个

public class Language implements Serializable {
   @Column(name = "isDefault")
   private Integer isDefault;

   @Column(name = "abbr")
   private String abbr;

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

   @Id
   @OneToOne(cascade = {CascadeType.ALL})
   @JoinColumn(name = "item_id")
   private Item itemId;
}

我的商品实体

public class Item implements Serializable {
   @Column(name = "c4i")
   private String c4i;

   @Column(name = "status")
   private Integer status=1;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id")
   private Integer id;
}

我怎么能解决这个问题。我错过了什么? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

在您的E-R图表中,item_info使用加入主键:item_id,language_item_id和type,但在您的映射中,您只是映射了item_id

答案 1 :(得分:0)

快乐新闻:)

我将 referencedColumnName 参数添加到 JoinColumn 注释,现在工作正常!

Last Entity看起来像这样。

public class ItemInfo implements Serializable {

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

    @Id
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "item_id", nullable = false)
    private Item itemId;

    @ManyToOne
    @JoinColumn(name = "language_item_id",**referencedColumnName = "item_id"**)
    private Language languageItemId;

    @Column(name = "type", nullable = false)
    private Integer type;
}

谢谢大家。祝你有愉快的一天。