类中的IllegalArgumentException:model.Category,属性的getter方法:id

时间:2016-08-21 17:29:42

标签: java hibernate h2

情况:我有两个关系oneToMany(类别oneToMany产品), 在创建像这样的产品时:

Category c1 = new Category("Printer");
    Category c2 = new Category("Scanner");
    Category c3 = new Category("Phone");
    Factory.getInstance().getCategoryDAO().addCategory(c1);//id1
    Factory.getInstance().getCategoryDAO().addCategory(c2);//id2
    Factory.getInstance().getCategoryDAO().addCategory(c3);//id3

    //Product(catID, name, price)
    Product p1 = new Product(2,"Panasonic", new BigDecimal("322.12"));
    Product p2 = new Product(2,"Samsung", new BigDecimal("700.01"));
    Product p3 = new Product(3,"NOKIA", new BigDecimal("12000.12"));

    Factory.getInstance().getProductDAO().addProduct(p1);
    Factory.getInstance().getProductDAO().addProduct(p2);
    Factory.getInstance().getProductDAO().addProduct(p3);

我遇到了这个例外:

    Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
авг 22, 2016 12:06:21 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id
авг 22, 2016 12:06:36 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id
авг 22, 2016 12:06:37 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id

班级类别:

    public class Category {

    private Integer id;
    private String name;
    private Set<Product> products;

    public Category(String name) {
        this.name = name;
        this.products = new HashSet<Product>(0);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Set<Product> getProducts() {
        return products;
    }

    public void setProducts(Set<Product> products) {
        this.products = products;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

类产品:

    public class Product {

    private Integer id;
    private Integer catId;
    private String name;
    private BigDecimal price;

    public Product(Integer catId, String name, BigDecimal price) {
        this.catId = catId;
        this.name = name;
        this.price = price;
    }

    public Integer getId() {
        return id;
    }

    public Integer getCatId() {
        return catId;
    }

    public String getName() {
        return name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setCatId(Integer catId) {
        this.catId = catId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}

和类别映射:

<class name="model.Category" table="categories">

    <id name="id" type="java.lang.Integer">
        <column name="CATEGORY_ID"/>
        <generator class="identity"/>
    </id>

    <property name="name" type="java.lang.String">
        <column name="CATEGORY_NAME" not-null="true" unique="true"/>
    </property>

    <set name="products" table="products" lazy="false" inverse="true" fetch="select">
        <key>
            <column name="CAT_ID" not-null="true"/>
        </key>
        <one-to-many class="model.Product"/>
    </set>

</class>

产品映射:

<hibernate-mapping>

<class name="model.Product" table="products">

    <id name="id" type="java.lang.Integer">
        <column name="PRODUCT_ID"/>
        <generator class="identity"/>
    </id>

    <property name="name" type="java.lang.String">
        <column name="PRODUCT_NAME"/>
    </property>

    <property name="price" type="java.math.BigDecimal" precision="2" scale="16" >
        <column name="PRODUCT_PRICE"/>
    </property>

    <many-to-one name="catId" class="model.Category" fetch="select">
        <column name="CAT_ID" not-null="true"/>
    </many-to-one>

</class>

1 个答案:

答案 0 :(得分:2)

我自己找到了解决方案。 我在Product class中将private Integer catId;替换为private Category category;