Hibernate错误列名无效

时间:2016-01-20 04:46:36

标签: spring hibernate

我试图在hibernate和spring中创建一对多和多对一的关系。

下面是我的产品类代码

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import java.io.Serializable;
@Entity
@Table(name="Category")
public class Category implements Serializable{

    @Id
    @Column(name="categoryId")
    @GeneratedValue
    private Integer categoryId;

    @Column(name="categoryName")
    private String categoryName;

    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinTable(
            name="CategoryProduct",
            joinColumns = @JoinColumn(name="categoryId"),
            inverseJoinColumns = @JoinColumn(name="productId")
    )
    public Set<Product> product;


    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Set<Product> getProduct() {
        return product;
    }

    public void setProduct(Set<Product> product) {
        this.product = product;
    }

}

下面是我的类别类

的代码
from facepy import GraphAPI  
key = 'My Access_Token'
a = GraphAPI(key)
response = a.get('Page_ID/posts')
print response

每当我运行代码时,我都会收到无效的列名'category_categoryId'。

我桌子的结构 产品的列为productId,poductName和productPrice category的列为categoryId,categoryName categoryproducts的列为categoryId和productId

1 个答案:

答案 0 :(得分:1)

您忘记将inverseJoinColumns放在产品类的@JoinTable中。这导致hibernate使用它们的joinTable约定<ClassName>_<columnName>。将其更改为

@JoinTable(
    name="CategoryProduct",
    joinColumns = @JoinColumn(name="productId"),
    inverseJoinColumns = @JoinColumn(name="categoryId")
)
private Category category;

但是,看看您的设计,您实际上在产品和类别之间创建了2个单向关联。如果你想创建双向关联,我建议你改变一面(类别),使用mappedBy

@OneToMany(mappedBy="category", cascade=CascadeType.ALL,fetch=FetchType.EAGER)
public Set<Product> product;

通常会达到相同的效果。希望它有所帮助!