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/