今天开发ssh项目遇到如下bug,下面贴出解决方案:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: cn.itcast.shop.review.Review column: pid (should be mapped with insert="false" update="false")
看一下代码:我在review类中加了个pid字段,pid是商品表product的主键,他作为review评论表的外键
public class Review {
private Integer reviewid;
private String re_name;
private String re_content;
private String re_time;
private Integer pid;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
//所属的商品类】
private Product product;
public Integer getReviewid() {
return reviewid;
}
public void setReviewid(Integer reviewid) {
this.reviewid = reviewid;
}
public String getRe_name() {
return re_name;
}
public void setRe_name(String re_name) {
this.re_name = re_name;
}
public String getRe_content() {
return re_content;
}
public void setRe_content(String re_content) {
this.re_content = re_content;
}
public String getRe_time() {
return re_time;
}
public void setRe_time(String re_time) {
this.re_time = re_time;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
错误就在于这个外键,外键pid是不需要重新再实体类中声明,因为在review.hbm.xml文件中我已经定义,如下:
<many-to-one name="product" class="cn.itcast.shop.product.Product" column="pid"></many-to-one>
这个many-to-one已经声明了外键,所以报错他会说repeated column(重复字段)
解决办法:
去掉所有外键的配置只保留many-to-one的那个!