I'm just testing something out.  Since I use EJB3 and need lazy initialization I'm making an attempt to see if passing an object with a collection of lazily-loaded elements in an IModel, from page-to-page, will prevent a LazyInitializationException (Hibernate).

I start here in the first page:

public class ViewBlog extends WebPage
{   
    public ViewBlog()
    {
        //get Blog in detached model
        IModel blogModel = new LoadableDetachableModel()
        {
            protected Object load()
            {
                return BlogProxy.getDefault(); //via proxy
            }
        };
       
        //add panel components
        add(new HeaderPanel("headerPanel", blogModel));
............

...and then in the HeaderPanel I do this:

public class HeaderPanel extends Panel
{   
    public HeaderPanel(String id, IModel blogModel)
    {
        super(id);
       
        Blog blog = (Blog)blogModel; //EXCEPTION HERE!
..............

I get this exception:

java.lang.ClassCastException: com.myapp.ui.ViewBlog$1
    at com.myapp.ui.panel.HeaderPanel.<init>(HeaderPanel.java :34)
    at com.myapp.ui.ViewBlog.<init>(ViewBlog.java:53)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java :39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
    at java.lang.Class.newInstance0 (Class.java:350)
    at java.lang.Class.newInstance(Class.java:303)

How do I down-cast to Blog (entity bean) from the IModel param?

Reply via email to