0

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>

4
  • Can you share the query please? Commented Aug 5, 2014 at 16:26
  • Can you share the mapping file as well? Commented Aug 5, 2014 at 16:31
  • What type(class) of variable do you assign the result of the above to? Commented Aug 5, 2014 at 16:36
  • @Priyesh I have tried both Request & ExternalRequest Commented Aug 5, 2014 at 16:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are correct, It is returning array of objects and I have added fetch now to get a single object, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.