Results 1 to 5 of 5

Thread: JPA Help!

  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation JPA Help!

    Greetings,

    I was studying about JPA for a while now and created a program wherein it will return all records on a particular table.
    When I run the program, the codes are below (written on Netbeans 6.9.1)
    I always get this exception.

    Java Code:
    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to application.Model
            at application.Main.main(Main.java:33)
    I've tried converting the object, models.get(index).getPersonNumber() to
    Java Code:
    String.valueOf(models.get(index).getPersonNumber())
    Yet the same exception.

    How is this possible?
    How can I resolve it?

    Here's the codes.
    persistence.xml
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
      <persistence-unit name="JPASamplePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>application.Model</class>
        <properties>
          <property name="hibernate.connection.username" value="root"/>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.password" value=""/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itce"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
      </persistence-unit>
    </persistence>
    Model.java
    Java Code:
    package application;
    
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="person", catalog="test", schema="")
    public class Model implements Serializable {
    
        public boolean isDeleted() {
            return deleted;
        }
    
        public void setDeleted(boolean deleted) {
            this.deleted = deleted;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getMiddleName() {
            return middleName;
        }
    
        public void setMiddleName(String middleName) {
            this.middleName = middleName;
        }
    
        public int getPersonNumber() {
            return personNumber;
        }
    
        public void setPersonNumber(int personNumber) {
            this.personNumber = personNumber;
        }
    
        @Id
        @GeneratedValue
        @Column(name="PersonNumber", length=9)
        private int personNumber;
        @Column(name="LastName", nullable=false, length=50)
        private String  lastName;
        @Column(name="FirstName", nullable=false, length=50)
        private String  firstName;
        @Column(name="MiddleName", nullable=true, length=50)
        private String  middleName;
        @Column(name="Deleted", nullable=false, length=1)
        private boolean deleted;
    
    }
    Main.java
    Java Code:
    package application;
    
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.EntityTransaction;
    import javax.persistence.Persistence;
    import javax.persistence.Query;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
                EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JPASamplePU");
                EntityManager entityManager = entityManagerFactory.createEntityManager();
                EntityTransaction entityTransaction = entityManager.getTransaction();
    
                entityTransaction.begin();
    
                Query query = entityManager.createNativeQuery("SELECT * FROM person");
                List<Model> models = query.getResultList();            
                for(int index=0; index<models.size(); index++) {                
                    System.out.println("Record #:     " + String.valueOf(index+1));
                    System.out.println("PersonNumber: " + models.get(index).getPersonNumber());
                    System.out.println("LastName:     " + models.get(index).getLastName());
                    System.out.println("FirstName:    " + models.get(index).getFirstName());
                    System.out.println("MiddleName:   " + models.get(index).getMiddleName());
                    System.out.println("Deleted:      " + models.get(index).isDeleted());
                }            
                entityManager.close();
                entityManagerFactory.close();
            } catch(Exception ex) {
                ex.printStackTrace();
            }            
        }   
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Post more of the stack trace, and indicate the lines it reveals.

    Line 33 is
    Java Code:
    ex.printStackTrace();
    D'uh.

    II don't do JavaEE, but if the next line down in the stack trace is 21, it's probably telling you that Query#getResultList() returns a List<Object> (which is the same as a non-generic List), not a List<Model>.

    db

  3. #3
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Quote Originally Posted by Darryl.Burke View Post
    II don't do JavaEE, but if the next line down in the stack trace is 21, it's probably telling you that Query#getResultList() returns a List<Object> (which is the same as a non-generic List), not a List<Model>.
    Isn't all Class either pre-defined or user-defined automatically inherits the Object class
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  4. #4
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Thread is solved. Just used TopLink instead of Hibernate
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by chyrl View Post
    Isn't all Class either pre-defined or user-defined automatically inherits the Object class
    SomeClass extends Object does not mean that List<SomeClass> extends List<Object>

    db

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •