-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: IllegalArgumentException
PostPosted: Sat Mar 04, 2006 10:03 pm 
Newbie

Joined: Mon Jan 30, 2006 3:14 am
Posts: 5
I am simply testing an "isa" relationship in a simple database design of Students "isa" Person. I am trying to simply save a Person, and then save that Person as a Student. It will save the Person no problem, but then when I save that Person object as a Student, it blows up with the errors Below

I have had this problem for almost a week now, and any help would be greatly appreciated.

Thanks in advance,
Sean




Hibernate version:
Hibernate3

Mapping documents:
<?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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Mar 01 00:17:40 CST 2006 -->
<hibernate-mapping package="com">

<class name="Person" table="person">
<id name="pid" column="pid" type="integer">
<generator class="increment"/>
</id>

<property name="ssn" column="ssn" type="integer" not-null="true" />
<property name="name" column="name" type="string" not-null="true" />

<set name="professorsSet" inverse="true">
<key column="pid"/>
<one-to-many class="Professors"/>
</set>

<set name="studentsSet" inverse="true">
<key column="pid"/>
<one-to-many class="Students"/>
</set>

<set name="studentsCoursesSet" inverse="true">
<key column="pid"/>
<one-to-many class="StudentsCourses"/>
</set>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
public static void main(String[] args)
{
// Step 1 - Create a new entity
Person p = new Person();
p.setName("Jerry Seinfeld");
p.setSsn(123);
Set stus = new HashSet();
//p.setStudentsSet(new HashSet());
Students stu = new Students(new Person());

try
{
// testForumCode();
// Step 3 - Get a Hibernate Session
Session session = SessionManager.currentSession();
// Step 4 - Persist entity to database
Transaction tx = session.beginTransaction();

session.save(p);

stu = new Students(p);
stu.setGrade("3");
//stus.add(stu);

p.setStudentsSet(stus);
p.getStudentsSet().add(stu);
//output("getStus = " + p.getStudentsSet());

session.save(stu);
tx.commit();

System.out.println("Save successful.");

}
catch (HibernateException e)
{
e.printStackTrace();
System.out.println("First Attempt failed.");
}
Full stack trace of any exception that occurs:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.AbstractStudents.person
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:70)
at org.hibernate.tuple.AbstractTuplizer.setIdentifier(AbstractTuplizer.java:130)
at org.hibernate.persister.entity.BasicEntityPersister.setIdentifier(BasicEntityPersister.java:2949)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:147)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at TEST.SaveTest.main(SaveTest.java:46)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
... 12 more

Name and version of the database you are using:
PostgreSQL 8.1
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 05, 2006 12:02 pm 
Regular
Regular

Joined: Sat Aug 28, 2004 4:15 pm
Posts: 61
Quote:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.AbstractStudents.person


Hibernate was expressing its anger that you don't have a proper setter for the person attribute.

Please post your domain model objects. Person, AbstractPerson, Student...whatever they are.

Joe

_________________
Joe W


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 05, 2006 2:05 pm 
Newbie

Joined: Mon Jan 30, 2006 3:14 am
Posts: 5
That's weird b/c I have Debugged that exact part of the code, and the Person object and its attributes seem to be set fine. Then again, I am a Hibernate newbie, so that presents a problem in itself.


My AbstractPerson is as follows:
Code:
/*
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Wed Mar 01 00:17:40 CST 2006 by MyEclipse Hibernate Tool.
*/
package com;

import java.io.Serializable;

/**
* A class that represents a row in the person table. You can customize the
* behavior of this class by editing the class, {@link Person()}. WARNING: DO
* NOT EDIT THIS FILE. This is a generated file that is synchronized by
* MyEclipse Hibernate tool integration.
*/
public abstract class AbstractPerson implements Serializable
{
   /**
    * The cached hash code value for this instance. Settting to 0 triggers
    * re-calculation.
    */
   private int hashValue = 0;

   /** The composite primary key value. */
   private java.lang.Integer pid;

   /** The value of the professorsSet one-to-many association. */
   private java.util.Set professorsSet;

   /** The value of the studentsSet one-to-many association. */
   private java.util.Set studentsSet;

   /** The value of the studentsCoursesSet one-to-many association. */
   private java.util.Set studentsCoursesSet;

   /** The value of the simple ssn property. */
   private java.lang.Integer ssn;

   /** The value of the simple name property. */
   private java.lang.String name;

   /**
    * Simple constructor of AbstractPerson instances.
    */
   public AbstractPerson()
   {
   }

   /**
    * Constructor of AbstractPerson instances given a simple primary key.
    *
    * @param pid
    */
   public AbstractPerson(java.lang.Integer pid)
   {
      this.setPid(pid);
   }

   /**
    * Return the simple primary key value that identifies this object.
    *
    * @return java.lang.Integer
    */
   public java.lang.Integer getPid()
   {
      return pid;
   }

   /**
    * Set the simple primary key value that identifies this object.
    *
    * @param pid
    */
   public void setPid(java.lang.Integer pid)
   {
      this.hashValue = 0;
      this.pid = pid;
   }

   /**
    * Return the value of the ssn column.
    *
    * @return java.lang.Integer
    */
   public java.lang.Integer getSsn()
   {
      return this.ssn;
   }

   /**
    * Set the value of the ssn column.
    *
    * @param ssn
    */
   public void setSsn(java.lang.Integer ssn)
   {
      this.ssn = ssn;
   }

   /**
    * Return the value of the name column.
    *
    * @return java.lang.String
    */
   public java.lang.String getName()
   {
      return this.name;
   }

   /**
    * Set the value of the name column.
    *
    * @param name
    */
   public void setName(java.lang.String name)
   {
      this.name = name;
   }

   /**
    * Return the value of the pid collection.
    *
    * @return Professors
    */
   public java.util.Set getProfessorsSet()
   {
      return this.professorsSet;
   }

   /**
    * Set the value of the pid collection.
    *
    * @param professorsSet
    */
   public void setProfessorsSet(java.util.Set professorsSet)
   {
      this.professorsSet = professorsSet;
   }

   /**
    * Return the value of the pid collection.
    *
    * @return Students
    */
   public java.util.Set getStudentsSet()
   {
      return this.studentsSet;
   }

   /**
    * Set the value of the pid collection.
    *
    * @param studentsSet
    */
   public void setStudentsSet(java.util.Set studentsSet)
   {
      this.studentsSet = studentsSet;
   }

   /**
    * Return the value of the pid collection.
    *
    * @return StudentsCourses
    */
   public java.util.Set getStudentsCoursesSet()
   {
      return this.studentsCoursesSet;
   }

   /**
    * Set the value of the pid collection.
    *
    * @param studentsCoursesSet
    */
   public void setStudentsCoursesSet(java.util.Set studentsCoursesSet)
   {
      this.studentsCoursesSet = studentsCoursesSet;
   }

   /**
    * Implementation of the equals comparison on the basis of equality of the
    * primary key values.
    *
    * @param rhs
    * @return boolean
    */
   public boolean equals(Object rhs)
   {
      if (rhs == null)
         return false;
      if (!(rhs instanceof Person))
         return false;
      Person that = (Person) rhs;
      if (this.getPid() == null || that.getPid() == null)
         return false;
      return (this.getPid().equals(that.getPid()));
   }

   /**
    * Implementation of the hashCode method conforming to the Bloch pattern
    * with the exception of array properties (these are very unlikely primary
    * key types).
    *
    * @return int
    */
   public int hashCode()
   {
      if (this.hashValue == 0)
      {
         int result = 17;
         int pidValue = this.getPid() == null ? 0 : this.getPid().hashCode();
         result = result * 37 + pidValue;
         this.hashValue = result;
      }
      return this.hashValue;
   }
}




Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 05, 2006 2:06 pm 
Newbie

Joined: Mon Jan 30, 2006 3:14 am
Posts: 5
My AbstractStudents is as follows:


Code:
/*
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Wed Mar 01 00:17:40 CST 2006 by MyEclipse Hibernate Tool.
*/
package com;

import java.io.Serializable;

/**
* A class that represents a row in the students table. You can customize the
* behavior of this class by editing the class, {@link Students()}. WARNING: DO
* NOT EDIT THIS FILE. This is a generated file that is synchronized by
* MyEclipse Hibernate tool integration.
*/
public abstract class AbstractStudents implements Serializable
{
   /**
    * The cached hash code value for this instance. Settting to 0 triggers
    * re-calculation.
    */
   private int hashValue = 0;

   /** The composite primary key value. */
   private Person person;

   /** The value of the simple grade property. */
   private java.lang.String grade;

   /**
    * Simple constructor of AbstractStudents instances.
    */
   public AbstractStudents()
   {
   }

   /**
    * Constructor of AbstractStudents instances given a simple primary key.
    *
    * @param person
    */
   public AbstractStudents(Person person)
   {
      this.setPerson(person);
   }

   /**
    * Return the simple primary key value that identifies this object.
    *
    * @return Person
    */
   public Person getPerson()
   {
      return person;
   }

   /**
    * Set the simple primary key value that identifies this object.
    *
    * @param person
    */
   public void setPerson(Person person)
   {
      this.hashValue = 0;
      this.person = person;
      
      System.out.println("Students.person.pid = " + this.person.getPid());
      System.out.println("Students.person.Name = " + this.person.getName());
      System.out.println("Students.person.SSN = " + this.person.getSsn());
      System.out.println("Students.person.StudentsSet = " + this.person.getStudentsSet().toString());
      
   }

   /**
    * Return the value of the grade column.
    *
    * @return java.lang.String
    */
   public java.lang.String getGrade()
   {
      return this.grade;
   }

   /**
    * Set the value of the grade column.
    *
    * @param grade
    */
   public void setGrade(java.lang.String grade)
   {
      this.grade = grade;
   }

   /**
    * Implementation of the equals comparison on the basis of equality of the
    * primary key values.
    *
    * @param rhs
    * @return boolean
    */
   public boolean equals(Object rhs)
   {
      if (rhs == null)
         return false;
      if (!(rhs instanceof Students))
         return false;
      Students that = (Students) rhs;
      if (this.getPerson() == null || that.getPerson() == null)
         return false;
      return (this.getPerson().equals(that.getPerson()));
   }

   /**
    * Implementation of the hashCode method conforming to the Bloch pattern
    * with the exception of array properties (these are very unlikely primary
    * key types).
    *
    * @return int
    */
   public int hashCode()
   {
      if (this.hashValue == 0)
      {
         int result = 17;
         int personValue = this.getPerson() == null ? 0 : this.getPerson()
               .hashCode();
         result = result * 37 + personValue;
         this.hashValue = result;
      }
      return this.hashValue;
   }
}





All my Student and Person classes do is simply extend their Abstract parent class.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 8:13 pm 
Regular
Regular

Joined: Sat Aug 28, 2004 4:15 pm
Posts: 61
Seanlon,

I apologize but I'm having a tough time following what is going on with what you've posted thus far.

Post all classes and mappings. Maybe the problem is already obvious but given the way things are named I am not able to quickly follow and provide commentary.

....in other news....

You should really consider looking into the concept of a rich domain model. One small example, consider how the Set of Student objects can be replaced by any domain model client given the class has public access on that method. This is possibly a dangerous practice as outsiders are free to put any implementation or instance of a Set that they like. Similarly, by providing all simple getters and setters you're exposed to problems like this all around. Do some googling for 'rich domain model' if you're interested.


joe

_________________
Joe W


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.