Page 1 of 1

Trouble extending entities

Posted: Wed Oct 22, 2014 10:58 pm
by miked0809
I am trying to extend the Product entity using the instructions in the Getting Started tutorial. When I start the app the new DB table is created as expected (e.g. CONE_PRODUCT.terms_and_conditions). However, when I navigate to any screen that references an attribute on either of the extended entities, I get a Thymeleaf error like this:

Code: Select all

Exception evaluating SpringEL expression: "termsAndConditions" (catalog/product:63)
Caused by:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "termsAndConditions" (catalog/product:63)
...
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'termsAndConditions' cannot be found on object of type 'org.broadleafcommerce.core.catalog.domain.ProductImpl' - maybe not public?

Here is my new interface (added to 'core' module):

Code: Select all

package com.mycompany.core.catalog.domain;

import org.broadleafcommerce.core.catalog.domain.Product;

public interface ConeProduct extends Product {

   public String getTermsAndConditions();
   
   public void setTermsAndConditions(String termsAndConditions);
}

Here is my extended entity implementation:

Code: Select all

package com.mycompany.core.catalog.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.broadleafcommerce.core.catalog.domain.ProductImpl;

@Entity
@Table(name = "CONE_PRODUCT")
public class ConeProductImpl extends ProductImpl implements ConeProduct {

   private static final long serialVersionUID = 1L;
   
   @Column(name = "TERMS_AND_CONDITIONS")
   protected String termsAndConditions;
   
   public String getTermsAndConditions() {
      return termsAndConditions;
   }
   
   public void setTermsAndConditions(String termsAndConditions) {
      this.termsAndConditions = termsAndConditions;
   }
}

persistence-core.xml:

Code: Select all

<persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>jdbc/web</non-jta-data-source>
        <class>com.comverse.core.domain.HCCustomerImpl</class>
        <class>com.mycompany.core.catalog.domain.ConeProductImpl</class>
        <exclude-unlisted-classes/>
    </persistence-unit>

product.html:

Code: Select all

<div><h3>Terms and Conditions</h3></div>
<div th:text="*{termsAndConditions}"></div>

Also tried this for product.html:

Code: Select all

<div><h3>Terms and Conditions</h3></div>
<div th:text="${product.termsAndConditions}"></div>

I have not added anything to applicationContext-entity.xml for the ConeProductImpl based on the comment that it is only necessary for entities like Customer, Fulfillment Group and Order. I did, however, try extending the Customer entity exactly how it was outlined in the Getting Started tutorial, and did add an entry for that in this file. Incidentally, I got the same type of Thymeleaf error for my custom Customer implementation, which is why I then tried extending the Product entity, thinking it was something related to applicationContext-entity.xml not getting merged properly.

I suspect this issue may have something to do with how I am building the modules, as I'm not really sure what the proper way to do it is. I have tried several different ways, but here is what I last tried, which I think should pretty much have nuked and rebuilt everything from scratch (via Eclipse):
    site-->jetty-stop
    Maven Parents-->DemoSite-->Run As-->Maven Clean
    Maven Parents-->DemoSite-->Run As-->Maven Install
    My Broadleaf Site-->core-->Run As-->Maven Clean
    My Broadleaf Site-->core-->Run As-->Maven Install
    My Broadleaf Site-->admin-->Run As-->Maven Clean
    My Broadleaf Site-->admin-->Run As-->Maven Install
    My Broadleaf Site-->site-->Run As-->Maven Clean
    My Broadleaf Site-->site-->Run As-->Maven Install
    ant-->admin-->build-app
    ant-->site-->build-app

Pretty sure most of that was redundant, but as I said I'm not real clear on the proper way to build the various modules.

Thanks for your help,
Mike

Re: Trouble extending entities

Posted: Tue Nov 04, 2014 11:51 pm
by phillipuniverse
Extensions work with database tables. If you want to have an instance of CONE_PRODUCT you need to make 2 inserts: 1 insert into BLC_PRODUCT and then another insert into the foreign key for CONE_PRODUCT:

INSERT INTO BLC_PRODUCT (PRODUCT_ID) VALUES (1);
INSERT INTO CONE_PRODUCT (PRODUCT_ID) VALUES (1);

If you don't do that, when Hibernate goes to query for Product, it will only pull back instances of ProductImpl. Only with this joining do you get back instances of ConeProductImpl.

In the admin when you create a product you can choose which instance type you want to create which is why you don't need anything in applicationContext-entity.xml; these aren't automatically created by the system like Order and FulfillmentGroup are.

By the way, if you want to turn all of the entries from BLC_PRODUCT into ConeProductImpl, you can do it with this SQL:

INSERT INTO CONE_PRODUCT (PRODUCT_ID) VALUES (SELECT PRODUCT_ID FROM BLC_PRODUCT)