无法找到具有逻辑名称的列:product

时间:2014-09-18 18:08:19

标签: java sql spring hibernate jpa

Hibernate抛出错误:

org.hibernate.MappingException: 
    Unable to find column with logical name: product in 
        org.hibernate.mapping.Table(product_part) 
    and its related supertables and secondary tables

实体类

@Entity
@Table(name = "product_part")
public class ProductPart implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @ManyToOne
    @javax.persistence.JoinColumn(name = "product", referencedColumnName = "product", nullable = false)
    private ProductPart productByProduct;

    @ManyToOne
    @javax.persistence.JoinColumn(name = "part", referencedColumnName = "part", nullable = false)
    private Part partByPart;
    }

MySQL表格

CREATE TABLE `product_part` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product` int(11) NOT NULL,
  `part` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_product` (`product`),
  KEY `fk_part` (`part`),
  CONSTRAINT `fk_product` FOREIGN KEY (`product`) REFERENCES `product` (`product`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_part` FOREIGN KEY (`part`) REFERENCES `part` (`part`) ON DELETE NO ACTION ON UPDATE NO ACTION
) 

有什么问题?如何解决?

更新

外键链接表:

CREATE TABLE `part` (
      `part` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(150) NOT NULL,
      PRIMARY KEY (`part`)
    )

    CREATE TABLE `product` (
      `product` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(150) NOT NULL,
      PRIMARY KEY (`product`)
)

1 个答案:

答案 0 :(得分:1)

我看到你的班级有

@ManyToOne
@javax.persistence.JoinColumn(name = "product", referencedColumnName = "product", nullable = false)
private ProductPart productByProduct;

这是Hibernate Self Join Annotations。有@ManyToOne但没有@OneToMany。表未定义产品'柱。 你需要添加

@OneToMany(mappedBy="productByProduct")
private Set<ProductPart > subProductPart  = new HashSet<ProductPart>();