简体   繁体   中英

IllegalArgumentException in class: model.Category, getter method of property: id

Situation: I have 2 classes with relation oneToMany (Category oneToMany Product), when creating Product like this:

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);

I came into this exception:

    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

Class Category:

    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;
    }
}

Class Product :

    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;
    }
}

And Category mapping:

<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>

And Product mapping:

<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>

I found solution by myself. I replaced private Integer catId; with private Category category; in Product class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM