【问题标题】:IllegalArgumentException in class: model.Category, getter method of property: id类中的 IllegalArgumentException:model.Category,属性的 getter 方法:id
【发布时间】:2023-03-18 13:43:01
【问题描述】:

情况:我有 2 个类与 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>

【问题讨论】:

    标签: java hibernate h2


    【解决方案1】:

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

    【讨论】:

    • 很棒的+1。错误消息没有多大帮助:)我删除了我的答案,因为没有用:)
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    相关资源
    最近更新 更多