facebook

IllegalArgumentException

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 15 posts - 1 through 15 (of 32 total)
  • Author
    Posts
  • #247423 Reply

    SEanLon11
    Member

    I have the following error for a simple database: IllegalArgumentException occurred while calling setter of sessionclasses.AbstractStudents.person

    Any general suggestions??

    #247425 Reply

    Brian Fernandes
    Moderator

    Hi,

    I’m afraid your description of the problem is too vague for us to help you.
    Were you using MyEclipse Database tools or is this an error within a program you were writing?

    If you answer all the questions here: Posting Guidelines, we’ll be able to help you better.

    Best,
    Brian.

    #247433 Reply

    SEanLon11
    Member

    Here is the part of the code where I am executing it. I simply generated the classes using MyEclipse. Students “isa” Person. I am simply trying to work on a basic program using “isa” relationships on a small scale so I can apply the concepts learned to my large program. I will post my tables in a seperate post right after this one.

        public static void main(String[] args)
        {
            // Step 1 - Create a new entity
            Person p = new Person();
            p.setName("Cosmo");
            p.setSsn(123);
    
            Students stu = null;
    
            try
            {
                // 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("1");
                System.out.println("=== " + stu.getGrade());
                
                session.save(stu);
                // session.save(course);
                tx.commit();
                System.out.println("Save successful.");
            }
            catch (HibernateException e)
            {
                e.printStackTrace();
                System.out.println("First Attempt failed.");
            }
            finally
            {
                try
                {
                    // Step 5 - close the session
                    SessionManager.closeSession();
                }
                catch (HibernateException e1)
                {
                    // do nothing
                }
            }
        }
    #247436 Reply

    SEanLon11
    Member

    Here is the simple SQL schema:

    
    --
    -- Target: PostgreSQL 
    -- Syntax: psql \i filename
    -- 
    -- Date  : Feb 27 2006 23:24
    -- Script Generated by Database Design Studio 2.21.3 
    --
    
    
    
    --
    -- Create Table    : 'courses'   
    -- course_ID       :  
    -- courseName      :  
    --
    CREATE SEQUENCE seq_courses_course_ID
        INCREMENT 1
        START 1;
    
    CREATE TABLE courses (
        course_ID      INTEGER DEFAULT NEXTVAL('seq_courses_course_ID') NOT NULL,
        courseName     TEXT NOT NULL,
    CONSTRAINT pk_courses PRIMARY KEY (course_ID));
    
    
    --
    -- Create Table    : 'person'   
    -- pid             :  
    -- ssn             :  
    -- name            :  
    --
    CREATE SEQUENCE seq_person_pid
        INCREMENT 1
        START 1;
    
    CREATE TABLE person (
        pid            INTEGER DEFAULT NEXTVAL('seq_person_pid') NOT NULL,
        ssn            INTEGER NOT NULL,
        name           TEXT NOT NULL,
    CONSTRAINT pk_person PRIMARY KEY (pid));
    
    
    --
    -- Create Table    : 'students'   
    -- pid             :  (references person.pid)
    -- grade           :  
    --
    CREATE TABLE students (
        pid            INTEGER NOT NULL,
        grade          TEXT NOT NULL,
    CONSTRAINT pk_students PRIMARY KEY (pid),
    CONSTRAINT fk_students FOREIGN KEY (pid)
        REFERENCES person (pid)
        MATCH FULL
        ON DELETE NO ACTION
        ON UPDATE CASCADE);
    
    
    --
    -- Create Table    : 'students_courses'   
    -- course_ID       :  (references courses.course_ID)
    -- pid             :  (references students.pid)
    --
    CREATE TABLE students_courses (
        course_ID      INTEGER NOT NULL,
        pid            INTEGER NOT NULL,
    CONSTRAINT pk_students_courses PRIMARY KEY (course_ID,pid),
    CONSTRAINT fk_students_courses2 FOREIGN KEY (course_ID)
        REFERENCES courses (course_ID)
        MATCH FULL
        ON DELETE NO ACTION
        ON UPDATE CASCADE,
    CONSTRAINT fk_students_courses FOREIGN KEY (pid)
        REFERENCES students (pid)
        MATCH FULL
        ON DELETE CASCADE
        ON UPDATE CASCADE);
    
    
    --
    -- Create Table    : 'profesors'   
    -- pid             :  (references person.pid)
    -- gradeTaught     :  
    --
    CREATE TABLE profesors (
        pid            INTEGER NOT NULL,
        gradeTaught    INTEGER NOT NULL,
    CONSTRAINT pk_profesors PRIMARY KEY (pid),
    CONSTRAINT fk_profesors FOREIGN KEY (pid)
        REFERENCES person (pid)
        MATCH FULL
        ON DELETE NO ACTION
        ON UPDATE CASCADE);
    
    
    --
    -- Permissions for: 'public'
    --
    GRANT ALL ON courses TO GROUP public;
    GRANT ALL ON person TO GROUP public;
    GRANT ALL ON students TO GROUP public;
    GRANT ALL ON students_courses TO GROUP public;
    GRANT ALL ON profesors TO GROUP public;
    
    
    
    #247437 Reply

    SEanLon11
    Member

    And here are the XML files for Person:

    
    <?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 Mon Feb 27 23:28:38 CST 2006                         -->
    <hibernate-mapping package="sessionclasses">
    
        <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="profesorsSet" inverse="true">
                <key column="pid"/>
                <one-to-many class="Profesors"/>
            </set>
     
            <set name="studentsSet" inverse="true">
                <key column="pid"/>
                <one-to-many class="Students"/>
            </set>
        </class>
        
    </hibernate-mapping>
    
    #247438 Reply

    SEanLon11
    Member

    And here is the XML file for Student, which (just like Person) was produced by MyEclipse without any updates from me:

    
    
    <?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 Mon Feb 27 23:28:39 CST 2006                         -->
    <hibernate-mapping package="sessionclasses">
    
        <class name="Students" table="students">
            <id name="person" column="pid" type="integer">
                <generator class="increment"/>
            </id>
     
            <property name="grade" column="grade" type="string"  not-null="true" />
     
            <set name="studentsCoursesSet" inverse="true">
                <key column="pid"/>
                <one-to-many class="StudentsCourses"/>
            </set>
        </class>
        
    </hibernate-mapping>
    

    Thanks a lot!! I would appreciate any help or suggestions.
    Sean

    P.S. Do you recomend any good hibernate books?

    #247444 Reply

    Riyad Kalla
    Member

    Book: Hibernate in Action, you can get it from Manning, even the e-book should be enough (and 1/2 price).

    As far as your problem, can you specify which portion of code that is executing, when you get that exception?

    #247454 Reply

    SEanLon11
    Member

    It blows up when I run my main method in one of my above posts (the only one with a public void main()), and it gives me the following stack trace:

    
    org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of sessionclasses.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.HibernateTest.main(HibernateTest.java:36)
    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
    

    I run the debugger, and it runs fine up until the line that looks like:

    session.save(stu);

    The only reason I have any System.out.println() statements is for debugging purposes, you can disregard all of them.

    Thanks in advance.

    #247457 Reply

    Riyad Kalla
    Member

    Hmm this needs some more investigative work on your end, but here are some tidbits:

    org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of sessionclasses.AbstractStudents.person

    Ok something funky with the argument passed to a setter on Person

    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)

    This makes me think it was trying to assign it an ID, it generated it then tried to set it and it failed.

    Caused by: java.lang.IllegalArgumentException: argument type mismatch

    And this confirms the first point.

    Try posting the code for Student, maybe it was generated not as an Integer but as something else. The mapping file looks fine.

    #247460 Reply

    SEanLon11
    Member

    Here is the AbstractStudents, which Students extends (there is no added functionality in Student):

    
    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Mon Feb 27 23:28:39 CST 2006 by MyEclipse Hibernate Tool.
     */
    package sessionclasses;
    
    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 studentsCoursesSet one-to-many association. */
        private java.util.Set studentsCoursesSet;
    
        /** 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)
        {
            System.out.println("aaaa");
            this.hashValue = 0;
            this.person = person;
            
            System.out.println("person.Name = " + person.getName());
            System.out.println("person.Name = " + person.getPid());
            System.out.println("person.Name = " + person.getSsn());
        }
    
        /**
         * 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;
        }
    
        /**
         * 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 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;
        }
    }
    

    I am very new to Hibernate and having trouble with the “isa” relationships, and I really appreciate your help.

    Sean

    #247461 Reply

    SEanLon11
    Member

    The “isa” relationship I am referring to is Students “isa” Person within my simple database design. Here is the AbstractPersons file:

    
    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Mon Feb 27 23:28:38 CST 2006 by MyEclipse Hibernate Tool.
     */
    package sessionclasses;
    
    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 profesorsSet one-to-many association. */
        private java.util.Set profesorsSet;
    
        /** The value of the studentsSet one-to-many association. */
        private java.util.Set studentsSet;
    
        /** 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 Profesors
         */
        public java.util.Set getProfesorsSet()
        {
            return this.profesorsSet;
        }
    
        /**
         * Set the value of the pid collection.
         * 
         * @param profesorsSet
         */
        public void setProfesorsSet(java.util.Set profesorsSet)
        {
            this.profesorsSet = profesorsSet;
        }
    
        /**
         * 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)
        {
            System.out.println("xxxxxx");
            this.studentsSet = studentsSet;
            System.out.println("TTTTTTTT");
        }
    
        /**
         * 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;
        }
    }
    
    #247463 Reply

    Riyad Kalla
    Member

    I’m confused, you said you had MyEclipse generate the mappings, but ti seems to me 95% of the code here you wrote… can you post the code for Students?

    #247464 Reply

    SEanLon11
    Member

    The only code that I have written is in my main() method, the other classes I have not touched at all. Here is student:

    
    
    /*
     * Created Mon Feb 27 23:28:39 CST 2006 by MyEclipse Hibernate Tool.
     */
    package sessionclasses;
    
    import java.io.Serializable;
    
    /**
     * A class that represents a row in the 'students' table. This class may be
     * customized as it is never re-generated after being created.
     */
    public class Students extends AbstractStudents implements Serializable
    {
        /**
         * Simple constructor of Students instances.
         */
        public Students()
        {
        }
    
        /**
         * Constructor of Students instances given a simple primary key.
         * 
         * @param person
         */
        public Students(Person person)
        {
            super(person);
        }
    
        /* Add customized code below */
    
    }
    
    #247466 Reply

    Riyad Kalla
    Member

    The only code that I have written is in my main() method, the other classes I have not touched at all.

    Ok thanks for the clarification, for some reason I got the impression you wrote all the Abstract implementation.

    Ok so the problem is that AbstractStudents does not extend AbstractPerson, which has your pid in it. So if you look at your Students hierarchy and flatten it out, you have no ID field, which Hibernate requires.

    Now if you look at your table definitions:


    — Create Table : ‘students’
    — pid : (references person.pid)
    — grade :

    CREATE TABLE students (
    pid INTEGER NOT NULL,
    grade TEXT NOT NULL,
    CONSTRAINT pk_students PRIMARY KEY (pid),
    CONSTRAINT fk_students FOREIGN KEY (pid)
    REFERENCES person (pid)
    MATCH FULL
    ON DELETE NO ACTION
    ON UPDATE CASCADE);

    your students table clearly has a PID value, but for some reason during the reverse engineering process that isn’t getting picked up. So if you backup to your exception at run time, it seems that Hibernate is trying to generate and set an ID for that field, but cannot find the setter for it.

    Does this sound right?

    #247468 Reply

    SEanLon11
    Member

    Yes, that sounds like that is the problem, but I was trying to figure out if it was something that had been generated or the database itself.

    your students table clearly has a PID value, but for some reason during the reverse engineering process that isn’t getting picked up. So if you backup to your exception at run time, it seems that Hibernate is trying to generate and set an ID for that field, but cannot find the setter for it.

    Does this mean that the reverse engineering process within MyEclipse is defective in this instance?? Or have I done something wrong within my database?

Viewing 15 posts - 1 through 15 (of 32 total)
Reply To: IllegalArgumentException

You must be logged in to post in the forum log in