0

我一直在从休眠 4 迁移到休眠 5。在休眠 4 中可以,但在休眠 5 中不起作用。

我得到了例外:

引起:org.hibernate.MappingException:外键 (FKf6eo63yo42ylh7vl5klap2eum:ProductParent [parent_id])) 必须具有与引用的主键相同的列数 (ProductParent [parent_id,product_id])

这是我的休眠映射:

@Entity 公共类 ProductParent 实现 Serializable {

@Id
@OneToOne()
@JoinColumn(name = "product_id")
private AbstractProduct product = new AbstractProduct();

@ManyToMany(cascade = ALL, fetch = EAGER)
@JoinTable(name = PRODUCTPARENT, joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "parent_id") })
private Set<ProductParent> parents = new HashSet<>();

和表结构:

CREATE TABLE productparent (  
  product_id bigint NOT NULL,  
  parent_id bigint,  
  CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES abstractproduct (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION,  
  CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES abstractproduct (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION,  
  CONSTRAINT product_id_parent_id_should_be_unique UNIQUE (product_id, parent_id)  
)

你能帮我吗?

4

1 回答 1

1

解决方案:

添加:

@OneToOne
@JoinColumn(name = PARENT_ID)
private AbstractProduct parent = new AbstractProduct();
于 2015-12-20T18:46:33.370 回答