-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Polymorphism problem! Help...
PostPosted: Tue Mar 01, 2005 7:05 pm 
Newbie

Joined: Tue Dec 28, 2004 6:08 pm
Posts: 1
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Mapping documents:

<hibernate-mapping package="com.previtec.questionario">
<class name="ControleAcao" table="ControleAcao">
<composite-id>
<key-many-to-one class="Pergunta" column="Pergunta" name="pergunta"/>
<key-many-to-one class="Acao" column="Acao" name="acao"/>
<key-many-to-one class="com.previtec.regra.Objeto" column="Objeto" name="objeto"/>
</composite-id>
</class>
</hibernate-mapping>

<hibernate-mapping package="com.previtec.questionario">
<class name="Acao" table="Acao" discriminator-value="not null">
<id column="Acao" name="acaoID" type="integer">
<generator class="identity"/>
</id>

<discriminator column="TipoAcao" type="integer"/>

<!--set inverse="true" lazy="true" name="controlesAcao" cascade="save-update">
<key column="Acao"/>
<one-to-many class="ControleAcao"/>
</set-->

<subclass name="GeracaoPergunta" discriminator-value="0" >
<many-to-one name="Pergunta" class="Pergunta" cascade="save-update" column="PerguntaGerada"/>
</subclass>
</class>
</hibernate-mapping>


Full stack trace of any exception that occurs:

Name and version of the database you are using: SQL Server 2000

Hi ... First, sorry for my english... I'm brazilian

I have a class Acao whith a subclass GeracaoPergunta (In the future will be more) and when I try to get a Acao from "ControleAcao" (Acao one of ControleAcao pks) I get a error casting it to GeracaoPergunta.
When I load Acao using session.load... the type retrieved is the subclass type ... Ok
When I get Acao from "ControleAcao" it's impossible to cast it to the subclass...

Acao is a abstract class.


Example that works:
Code:
public void testInheritence() {
        try {
            Session s = Ambiente.getInstance().getSession();
            Object object = s.load(Acao.class, 3);
            assertNotNull(object);
           
            System.out.println("Pro Christian acreditar:" + object.getClass().toString());
            if (!(object instanceof GeracaoPergunta))
            {
                fail("Classe deveria ser do tipo GeracaoPergunta");
            }
               
           
        } catch (Exception err) {
            err.printStackTrace();
        }
    }


Example that not works:

Code:
    public void testAcoes() {
        Session s = Ambiente.getInstance().getSession();
        Pergunta p = (Pergunta) s.load(Pergunta.class, 4);
        Objeto o = (Objeto) s.load(Objeto.class, 8);
       
        List controles = ControleAcao.getDAO().getControleAcaoList(p, o);
        for (Object controle : controles) {
            Acao acao = controle.getAcao();

            if (!(acao instanceof GeracaoPergunta)) {
                fail("Must be GeracaoPergunta... but is: " + acao.getClass().toString());
            }

        }
    }


I'm going crazy with that !! :/

Any help will be apreciated, thanks
Marcelo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.