Hibernate and IllegalArgumentException

300 views Asked by At

My hibernate mapping gives an error of:

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

I cannot seem to figure out where it occurs... can someone help me out?

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

my Category class.

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

last few log4j lines:

 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

How to fix this persistent error? I have been staring at my screen for hours now.

1

There are 1 answers

1
Mudassar On BEST ANSWER

Your mapping is wrong in Category.hbm.xml

It should be

<one-to-many class="opdracht5.Advertentie"/>

ref link https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html