-->
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.  [ 3 posts ] 
Author Message
 Post subject: Need Help: Visitor Implementation causing ClassCastException
PostPosted: Fri May 30, 2008 9:59 am 
Newbie

Joined: Fri May 30, 2008 9:35 am
Posts: 3
I am getting a class cast exception when using the Visitor Patter. I am pretty sure I have coded things correctly for it to work.

I have a collection of organizations that i need to be able to fetch from an Oracle DB. They are all stored on the same table, but are abstracted into different Java classes through hibernate.

The code that is doing the fetching and then using the visitor to get the proper object is:
Code:
CfgOrg brokerOrg = orgDAO.getOrgByOasysId(message.getOasysAcronym());
if(brokerOrg.isBrokerOrg())
{
    return brokerOrg.getBrokerOrg().getMpid();
}



The exception (shown below) is thrown when brokerOrg.getBrokerOrg() is called.
Code:
Caused by: java.lang.ClassCastException: com.itp.gt.data.org.generated.CfgOrg$$EnhancerByCGLIB$$36fb9bea
        at com.itp.gt.data.org.generated.CfgOrg$$EnhancerByCGLIB$$36fb9bea.getBrokerOrg(<generated>)




To make things simpler, all the visitor functionality has been coded in an abstract class, AbstractOrg, which all the specific org classes extend.

Code:
public abstract class AbstractOrg
{
    public abstract CfgOrgHelper accept(CfgOrgHelper visitor);// implemented in subclasses

    public boolean isClientOrg(){ return accept(new CfgOrgHelper()).isClientOrg(); }
    public boolean isBrokerOrg(){ return accept(new CfgOrgHelper()).isBrokerOrg(); }
    public boolean isSystemOrg(){ return accept(new CfgOrgHelper()).isSystemOrg(); }
    public boolean isIntermediateOrg(){ return accept(new CfgOrgHelper()).isIntermediateOrg(); }
    public boolean isBankOrg(){ return accept(new CfgOrgHelper()).isBankOrg(); }
    public boolean isCounterpartyOrg(){ return accept(new CfgOrgHelper()).isCounterpartyOrg(); }
    public boolean isFileHubClientOrg(){ return accept(new CfgOrgHelper()).isFileHubClientOrg(); }

    public CfgClientOrg getClientOrg(){ return accept(new CfgOrgHelper()).getClientOrg(); }
    public CfgBrokerOrg getBrokerOrg(){ return accept(new CfgOrgHelper()).getBrokerOrg(); }
    public CfgSystemOrg getSystemOrg(){ return accept(new CfgOrgHelper()).getSystemOrg(); }
    public CfgIntermediateOrg getIntermediateOrg(){ return accept(new CfgOrgHelper()).getIntermediateOrg(); }
    public CfgBankOrg getBankOrg(){ return accept(new CfgOrgHelper()).getBankOrg(); }
    public CfgCounterpartyOrg getCounterpartyOrg(){ return accept(new CfgOrgHelper()).getCptyOrg(); }
    public CfgFileHubClientOrg getFileHubClientOrg(){ return accept(new CfgOrgHelper()).getFileHubClientOrg(); }


    //============================= Visitor Implementation ===================================
    protected class CfgOrgHelper implements CfgOrgVisitor {
        private CfgBrokerOrg brokerOrg;
        private CfgClientOrg clientOrg;
        private CfgSystemOrg systemOrg;
        private CfgCounterpartyOrg cptyOrg;
        private CfgBankOrg bankOrg;
        private CfgIntermediateOrg intermediateOrg;
        private CfgFileHubClientOrg fileHubClientOrg;


        public CfgBrokerOrg getBrokerOrg() { return brokerOrg; }
        public CfgClientOrg getClientOrg() { return clientOrg; }
        public CfgSystemOrg getSystemOrg() { return systemOrg; }
        public CfgCounterpartyOrg getCptyOrg() { return cptyOrg; }
        public CfgBankOrg getBankOrg() { return bankOrg; }
        public CfgIntermediateOrg getIntermediateOrg() { return intermediateOrg; }
        public CfgFileHubClientOrg getFileHubClientOrg() { return fileHubClientOrg; }


        public void visit(CfgOrg  org){
            throw new RuntimeException("This method has to be implemented in all subclasses!!");
        }

        public void visit(CfgBrokerOrg  broker){ this.brokerOrg = broker; }
        public void visit(CfgClientOrg  client){ this.clientOrg = client; }
        public void visit(CfgCounterpartyOrg  cpty){ this.cptyOrg = cpty; }
        public void visit(CfgBankOrg  bank){ this.bankOrg = bank; }
        public void visit(CfgIntermediateOrg  intermediate){ this.intermediateOrg = intermediate; }
        public void visit(CfgSystemOrg  sys){ this.systemOrg = sys; }
        public void visit(CfgFileHubClientOrg  fileHub){ this.fileHubClientOrg = fileHub; }

        public boolean isBrokerOrg(){ return this.brokerOrg != null; }
        public boolean isClientOrg(){ return this.clientOrg != null; }
        public boolean isSystemOrg(){ return this.systemOrg != null; }
        public boolean isIntermediateOrg(){ return this.intermediateOrg != null; }
        public boolean isBankOrg(){ return this.bankOrg != null; }
        public boolean isCounterpartyOrg(){ return this.cptyOrg != null; }
        public boolean isFileHubClientOrg(){ return this.fileHubClientOrg != null; }
    }
}



The subclass CfgBrokerOrg's implementaion of accept:
Code:
public CfgOrgHelper accept(CfgOrgHelper visitor)
{
    visitor.visit(this);
    return visitor;
}



Does anyone see anything incorrect in this implementation. I would greatly appreciate any help resolving this issue. Thank you.


Last edited by hareshp on Fri May 30, 2008 12:32 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 12:21 pm 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
Hi,

I don't quite follow your pattern but maybe your issue has got to do the following :

If you use polymorphism like let's say :

public class MySubclass extends MySuperclass {
}

and you map your MySuperclass as proxy with a lazy="true", then Hibernate will proxy that association. This means that when you navigate the object graph and get a MySuperclass instance, this object will actually be a hibernate specific subclass of MySuperclass but NOT an instance of MySubclass. Therefore, any attempts to upcast this object to its subclass will fail.

As I said, I am not sure this relates to your issue but it's a thought :)

_________________
savakos


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 12:39 pm 
Newbie

Joined: Fri May 30, 2008 9:35 am
Posts: 3
I'm not trying to cast, I'm following the proxy visitor pattern:
http://www.hibernate.org/280.html

The hierarchy of my class structure is:

AbstractOrg <-- CfgOrg <-- CfgBrokerOrg


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

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.