2016-08-21 90 views
1

情况:我有2个教学班,一对多的关系(一对多类产品), 当创建产品是这样的:抛出:IllegalArgumentException类:model.Category,财产的getter方法:ID

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> 

回答

2

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

+1

很棒的+1。该错误消息是没有什么非常有用的:)我删除我的答案没有用:) – davidxxx

相关问题