简体   繁体   中英

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>

[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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM