java - 无法找到逻辑名称为 : product 的列

标签 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`)
)

最佳答案

我看到你们类有

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

这是 Hibernate 自连接注释。有@ManyToOne,但没有@OneToMany。表未定义“产品”列。 您需要添加

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

关于java - 无法找到逻辑名称为 : product 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919531/

相关文章:

java - 我可以在 Firefox 和 Chrome 中使用以下代码上传图像,但 Edge 中出现错误 :

php - Codeigniter 中的多连接数据库

java - Spring 测试对于不安全的 URL 返回 401

java - 使用 AuthenticationFailureHandler 在 Spring Security 中自定义身份验证失败响应

java - 为什么这个java计算器给我这个答案?

java - 提交新任务时取消当前任务的 ExecutorService

java - Elasticsearch启动错误——堆大小

sql - VBA for Excel 2016 中的 ADODB 连接超时 - 如何检查连接是否仍处于事件状态?

sql - 根据同一表中的主记录插入缺失记录

java - Spring 通配符不起作用