msmani
msmani

Reputation: 720

Hibernate subclass query : Java.lang.object cannot be cast to, ERROR

I have three Java Class, A which is the parent and B & C are subclasses of A. I have a Hibernate mapping file for A where I have mapped B & C using joined-subclass. Now when I try to query C I get [Ljava.lang.Object; cannot be cast to A. The query that Hibernate generates is correct but why it is not allowing the casting to A?

I have tried the following queries, both result in the same error.

session.createQuery("from Request as req inner join req.category where req.class=Externalrequest and req.requestId=:id");

session.createQuery("from ExternalRequest as ereq inner join ereq.category where ereq.requestId=:id");

Where Request is the parent class and ExternalRequest & InternalRequest are the child class.

And here is the basic structure of my mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Request" table="request" schema="public">
    <id name="requestId" type="integer">
        <column name="request_id" />
        <generator class="sequence" >
            <param name="sequence">request_request_id_seq</param>
        </generator>
    </id>


    <many-to-one name="category" column="category"  class="RequestCategory" />


     <joined-subclass name="ExternalRequest" table="external_request">
            <key column="request_id"/>
            .........

     </joined-subclass>

      <joined-subclass name="InternalRequest" table="internal_request">
            <key column="request_id"/>
            .......
     </joined-subclass>
</class>

Upvotes: 0

Views: 892

Answers (1)

Priyesh
Priyesh

Reputation: 2071

[Ljava.lang.Object; is the string representation of an array of Objects. I think what is happening is that you are trying to assign the result of the query, that is an array of Request or ExternalRequest, to a variable of class Request or ExternalRequest.

Upvotes: 1

Related Questions