Wicia
Wicia

Reputation: 565

Hibernate - java.lang.ClassCastException

I have strange problem which concerns getting objects from SQLLite DB. When I'am trying to get object from list I get following error:

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to DataBase.Hibernate.Entities.Client.ClientEntity

Any suggestions?

Mapping file:

<?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="DataBase.Hibernate.Entities.Client.ClientEntity" table="Clients">
      <meta attribute="class-description">
         This class contains the client detail. 
      </meta>
      <id name="id" type="int" column="ID">
         <generator class="native"/>
      </id>
      <property name="name" column="Name" type="string"/>
      <property name="surname" column="Surname" type="string"/>
      <property name="contact" column="Contact" type="string"/>
  </class>

  <sql-query name="ClientEntity.findByID">
    <![CDATA[
        select * from Clients AS c where c.ID =:id
    ]]>
   </sql-query>

   <sql-query name="ClientEntity.getAll">
    <![CDATA[
        select * from Clients
    ]]>
   </sql-query>

</hibernate-mapping>

ClientEntity class:

package DataBase.Hibernate.Entities.Client;

import java.io.Serializable;

public class ClientEntity implements Serializable{

    private int id;
    private String name;
    private String surname;
    private String contact; 

    public ClientEntity(String name, String surname, String contact) {
        this.name = name;
        this.surname = surname;
        this.contact = contact;
    }

    public ClientEntity() {}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }
}

Method for getting objects from table Clients

HibernateManager hibernate = HibernateManager.beginWork();
List<ClientEntity> list = hibernate.getNamedQuery(
Queries.GET_CLIENT_BY_ID).setInteger(Columns.ID, aClientID).list();
hibernate.endWork();

Queries.GET_CLIENT_BY_ID is variable which contains name of "named query" from ClientEntity.hbm.xml file ("ClientEntity.findByID").

Upvotes: 0

Views: 1946

Answers (2)

Wicia
Wicia

Reputation: 565

Ok, I get it. I have done small correction. But after all I have problem with Null Pointer Exception...

Project source

Problem occurs in class T4esty in method listClients.

Property hibernate.connection.url is not avaliable when program starts, but set "dynamically" after small initialization. Also data base is created each time program starts.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691625

You need to learn HQL, and use it instead of using SQL. Your 2 queries should be HQL queries:

select c from ClientEntity c where c.id =:id
select c from ClientEntity

Note that the firsqt query is useless, since you just need to use

session.get(ClientEntity.class, id)

to get a client by its ID.

Upvotes: 2

Related Questions