-1

I was trying to fetch hql query result assigned to custom class by casting the Object reference. but it throws for exception. But i have seen developers casting object reference returned from query to custom class reference without any issues.

What i wanted is,

//Address class

package car;

public class Address {

private int addId;    
private String city;
private String country;

    public String getCity() {
        return city;
    }

    public int getAddId() {
        return addId;
    }

    public void setAddId(int addId) {
        this.addId = addId;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}

//Hibernate mapping

<?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 = "car.Address" table = "studentAddress">
        <id name = "addId">
            <generator class = "assigned"/>
        </id>
        <property name = "city" column = "city"/>
        <property name = "country" column = "country" />
    </class>

</hibernate-mapping>

//Test class

public class Main {

    public static void main(String[] args) {


            Configuration cfg = new Configuration();
            cfg.configure("retail\\resources\\hibernate.cfg.xml");
            SessionFactory s = cfg.buildSessionFactory();
            Session ss = s.openSession();
            Transaction t = ss.beginTransaction();

            Student stu = new Student();
            stu.setSid(11);
            stu.setName("Thanweer");
            stu.setAge(28);

            Address a = new Address();
            a.setAddId(22);
            a.setCity("Colombo");
            a.setCountry("Sri Lanka");

            Query q = ss.createQuery("select city,country from Address a where a.city = 'Colombo'" );
            Address a2 = (Address)q.uniqueResult();
            System.out.println(a2.getAddId()+" "+a2.getCity()+" "+a2.getCountry());

    }

}

Exception Occurs is :

INFO: HHH000232: Schema update complete
Hibernate: select address0_.city as col_0_0_, address0_.country as col_1_0_ from studentAddress address0_ where address0_.city='Colombo'
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to car.Address
    at test.Main.main(Main.java:36)
user882813
  • 813
  • 5
  • 16
  • 1
    Edit the error into your post. – Xantium Dec 14 '17 at 22:36
  • 1
    Please add the code you have tried – wlh Dec 14 '17 at 22:46
  • Have you checked that what was returned? Maybe it's a null reference. – мајдејсаремех Dec 14 '17 at 22:58
  • Nope i checked it is returning Object. – Farhan Dulip Dec 14 '17 at 23:00
  • When i try printing out the object it is returning object memory address so it is not null. The issue is i cant cast the Object to Address – Farhan Dulip Dec 14 '17 at 23:02
  • *but it throws for exception* I see no exception here – Antoniossss Dec 15 '17 at 00:10
  • Try commenting out the problem line for now and replace it with this: `Object a2 = q.uniqueResult(); System.out.println(a2.getClass().getName());` Then tell us what you see. – mypetlion Dec 15 '17 at 01:09
  • It says, Dec 15, 2017 6:45:21 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: select address0_.city as col_0_0_, address0_.country as col_1_0_ from studentAddress address0_ where address0_.city='Colombo' [Ljava.lang.Object; – Farhan Dulip Dec 15 '17 at 01:16
  • The schema update is triggered due hibernate property which is pointing to update the database making a serious of ddl statement on a wrong schema. That might fail and you need to recreate it by dropping a database or configure a corresponding option which is doing it automatically. – Roman C Dec 20 '17 at 18:13

1 Answers1

1

Following HQL

select city,country from Address a where a.city = 'Colombo'

selects List of Object arrays. Thats why [Ljava.lang.Object;For further details please refer to this question.

In each array index 0 contains String city and index 1 contains String country. With uniqueResult() result is no more list, but simply array of objects. When Address is preferred, one should select Address as follows:

Query q = ss.createQuery("select a from Address a where a.city = 'Colombo'" );
Address a2 = (Address)q.uniqueResult();
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135