Page 1 of 1

Cannot extend Category

Posted: Tue Feb 19, 2013 7:56 am
by broadleafer
Hey guys, I can't seem to extend Category class. I ran a very basic test to extend Category and Customer.

On calling CustomerState.getCustomer() MyCustomerImpl is returned as expected.
But the following throws a ClassCastException

Code: Select all

MyCategoryImpl category = (MyCategoryImpl) request.getAttribute(CategoryHandlerMapping.CURRENT_CATEGORY_ATTRIBUTE_NAME);


Code: Select all

java.lang.ClassCastException: org.broadleafcommerce.core.catalog.domain.CategoryImpl cannot be cast to com.mycompany.domain.MyCategoryImpl


Here is my very basic broadleaf setup.

MyCategoryImpl.java

Code: Select all

@Entity
@Table(name = "MY_CATEGORY")
public class MyCategoryImpl extends CategoryImpl implements Serializable {

    @Column(name = "TEST")
    protected String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}


MyCustomerImpl.java

Code: Select all

@Entity
@Table(name = "MY_CUSTOMER")
public class MyCustomerImpl extends CustomerImpl implements Serializable {

     @Column(name = "TEST")
    protected String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}


persistence.xml file has

Code: Select all

<class>com.mycompany.domain.MyCustomerImpl</class>
    <class>com.mycompany.domain.MyCategoryImpl</class>


and

applicationContext-entity.xml

Code: Select all

<bean id="org.broadleafcommerce.profile.core.domain.Customer" class="com.mycompany.domain.MyCustomerImpl" scope="prototype"/>
<bean id="org.broadleafcommerce.core.catalog.domain.Category" class="com.mycompany.domain.MyCategoryImpl" scope="prototype"/>
   


The application starts fine and the log has the following.

Is there a reason broaleaf does not allow extending the Category class ? Or am I doing something wrong here ?

Code: Select all

[ INFO] 22:46:24 AnnotationBinder - Binding entity from annotated class: com.mycompany.domain.MyCustomerImpl
...
...
[ INFO] 22:46:33 DefaultListableBeanFactory - Overriding bean definition for bean 'org.broadleafcommerce.profile.core.domain.Customer': replacing [Generic bean: class [org.broadleafcommerce.profile.core.domain.CustomerImpl]; scope=prototype; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [bl-profile-applicationContext-entity.xml]] with [Generic bean: class [com.mycompany.domain.MyCustomerImpl]; scope=prototype; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext-entity.xml]]

Re: Cannot extend Category

Posted: Thu Feb 21, 2013 5:52 pm
by phillipuniverse
Nope, no reason broadleaf doesn't want to extend the Category class. Looks like you are doing everything correct.

Your problem is probably a data one. This post will probably be able to shed some light on your issue: viewtopic.php?f=11&t=1625

In short, I doubt you are importing data into the MY_CATEGORY table (at least the primary keys from BLC_CATEGORY). Because of this, Hibernate assumes that you do not have any instances of MyCategoryImpl hanging around, and thus does not return any instances of them.

The reason your situation works for customer is because Customers are created by the system (whenever you invoke customerService.createCustomer()) where the bean override is looked up. Categories (and the rest of the catalog) are not usually created automatically by the system; you usually include them in initial import scripts or create them via the admin.

Re: Cannot extend Category

Posted: Tue Feb 26, 2013 1:12 am
by broadleafer
Thanks phillip ... that solved the problem.