-->
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.  [ 2 posts ] 
Author Message
 Post subject: Lazy loading and polimorphism
PostPosted: Wed Nov 16, 2005 12:37 pm 
Newbie

Joined: Wed Oct 05, 2005 2:41 am
Posts: 1
Hi all,

As far as I understand, instanceof and objects casting are not recommended to use with polimorphic collections, because of lazy loading proxies.

Proxy Visitor pattern seems to be rather complicated.
My question is: does the following simple code solve the problem? What is happening, when I call method "asA" of proxy? Does it depend from inheritance mapping type (table per concrete class, etc.)?
Thank you for any answers.

Code:
abstract  class AbstractClass {
  public boolean isA() {
    return false;
  }
  public boolean isB() {
    return false;
  }
  public A asA() {
    throw new UnsupportedOperationException();
  }
  public B asB() {
    throw new UnsupportedOperationException();
  }
}

class A extends AbstractClass {
  public boolean isA() {
    return true;
  }
  public A asA() {
    return this;
  }
  public void methodOfA() {
  }
}

class B extends AbstractClass {
  public boolean isB() {
    return true;
  }
  public B asB() {
    return this;
  }
  public void methodOfB() {
  }
}

...

Collection lazyCollection = s.createCriteria(AbstractClass.class).list();
for(Iterator iterator = lazyCollection.iterator();iterator.hasNext();) {
  AbstractClass abstractClass = (AbstractClass) iterator.next();
  if (abstractClass.isA())
    abstractClass.asA().methodOfA();
  else if (abstractClass.isB())
    abstractClass.asB().methodOfB();
}


Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 9:03 pm 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
Well, in general, if you're iterating a collection and need to check the concrete type of the things in the collection, you're not using polymorphism in the first place. This is actually kind of anti-polylmorphism. Polymorphism would be iterating a collection and calling the "same" method on every object in the collection, but each object doing something different as a result of the method call. You should also not have an abstract class as the top of your inheritance hierarchy. That would be the job of an interface. Interfaces are for polymorphism, and abstract classes are a convenient spot for common code. So to make a long story short, try something like this:

Code:
public interface Foo {
    void doSomething();
}

public abstract class AbstractFoo {
    protected int toes; // because all Foos have some number of toes
    public int getToes() { return toes; }
}

public class FiveToedFoo extends AbstractFoo implements Foo {
    public FiveToedFoo() { this.toes = 5; }
    public void doSomething() {
        this.toes++; // Grow another toe
    }
}

public class NineToedFoo extends AbstractFoo implements Foo {
    private int ears = 2;
    public NineToedFoo() { this.toes = 9; }
    public void doSomething() {
        this.toes--;
        this.ears++; // Change a toe into an ear
    }
}


See? So ultimately, I'd first look at coming up with a better design if you have the liberty to do so. This will greatly increase the modifiability, maintainability, and all those other -ilities, but most importantly, the simplicity of your code. Finding a graceful design early will make it much easier in the long run rather than trying to force something to work which is not so graceful to begin with.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.