Hibernate和IllegalArgumentException

时间:2015-06-23 13:30:51

标签: java hibernate hbmxml

我的hibernate映射给出了错误:

Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of opdracht5.Category.id

我似乎无法弄清楚它发生在哪里......有人可以帮助我吗?

我的Category.hbm.xml:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<hibernate-mapping>  
  <class name="opdracht5.Category" table="Category">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <set name="advertenties" cascade="all">
       <key column="CATEGORY_ID"></key>
      <one-to-many class="opdracht5.Category"/>
    </set>       
  <property name="naam" type="string"></property> 

</class>  

</hibernate-mapping>  

我的分类课程。

public class Category {
private String naam;
private long id;
private Set<Advertentie> advertenties;

public Category() {}
public Category(String naam){
    this.naam = naam;
}

public void setNaam(String naam){
    this.naam = naam;
}
public void setAdvertenties(Set<Advertentie> advertenties){
    this.advertenties = advertenties;
}
public Set<Advertentie> getAdvertenties(){
    return advertenties;
}

public String getNaam(){
    return naam;
}
public void setId(long id){
    this.id = id;
}

public long getId(){
    return id;
}
}

Advertentie.hbm.xml

 <?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="opdracht5.Advertentie" table="Advertentie">  
   <id name="id">  
 <generator class="native"></generator>  
</id>  

<property name="naam"></property>  
<property name="beschrijving"></property>  
<property name="startPrijs"></property>
<property name="actief"></property>
<property name="StartDate"></property>
<many-to-one name="categoryNaam" class="opdracht5.Category"
        column="CATEGORY_ID" not-null="true"/>
  </class>  

 </hibernate-mapping>  

Advertentie.java

package opdracht5;
import java.util.*;
public class Advertentie {
private String naam;
private String beschrijving;
private int startPrijs;
private Boolean actief;
private Date StartDate;
private Category categoryNaam;
private long id;
public Advertentie() {
    // TODO Auto-generated constructor stub
}

public Advertentie(String naam, String beschrijving, int startPrijs, Boolean actief, Date StartDate, Category categoryNaam){
    this.naam = naam;
    this.beschrijving = beschrijving;
    this.startPrijs = startPrijs;
    this.actief = actief;
    this.StartDate = StartDate;
    this.categoryNaam = categoryNaam;
}
public Category getCategoryNaam(){
    return categoryNaam;
}
public void setCategoryNaam(Category categoryNaam){
    this.categoryNaam= categoryNaam;
}
public String getNaam(){
    return naam;
}
public String getBeschrijving(){
    return beschrijving;
}
public int getStartPrijs(){
    return startPrijs;
}
public Boolean getActief(){
    return actief;  
}
public Date getStartDate(){
    return StartDate;
}
public long getId(){
    return id;
}
public void setId(long id){
    this.id = id;
}
public void setNaam(String naam){
    this.naam = naam;
}
public void setBeschrijving(String beschrijving){
    this.beschrijving = beschrijving;
}
public void setStartPrijs(int startPrijs){
    this.startPrijs = startPrijs;
}
public void setActief(Boolean actief){
    this.actief = actief;
}
public void setStartDate(Date StartDate){
    this.StartDate = StartDate;
}
}

最后几条log4j行:

 DEBUG org.hibernate.event.def.AbstractSaveEventListener  - generated      identifier: 1, using strategy: org.hibernate.id.IncrementGenerator
 **strong text**10022 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - saving      [opdracht5.Category#1]
 **strong text**10025 [main] DEBUG org.hibernate.engine.Cascade  - processing cascade ACTION_SAVE_UPDATE for: opdracht5.Category
 **strong text**10025 [main] DEBUG org.hibernate.engine.Cascade  - done processing cascade ACTION_SAVE_UPDATE for: opdracht5.Category
 **strong text**10033 [main] DEBUG org.hibernate.event.def.WrapVisitor       - Wrapped collection in role: opdracht5.Category.advertenties
 **strong text**10037 [main] DEBUG org.hibernate.engine.Cascade  - processing cascade ACTION_SAVE_UPDATE for: opdracht5.Category
 **strong text**10037 [main] DEBUG org.hibernate.engine.Cascade  - cascade ACTION_SAVE_UPDATE for collection: opdracht5.Category.advertenties
 **strong text**10037 [main] DEBUG org.hibernate.engine.CascadingAction  - cascading to saveOrUpdate: opdracht5.Category
 **strong text**10038 [main] ERROR org.hibernate.property.BasicPropertyAccessor  - IllegalArgumentException in      class: opdracht5.Category, getter method of property: id

如何修复此持久性错误? 我现在一直盯着我的屏幕几个小时。

1 个答案:

答案 0 :(得分:0)

您在Category.hbm.xml中的映射是错误的

应该是

<div class="attributes">
                @Html.Raw(item.AttributeInfo)
            </div>

参考链接 https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html