Page 1 of 1

PresentationCollection for ManyToMany without mappedBy

Posted: Thu Mar 13, 2014 3:46 am
by pokemon007
Often the time @ManyToMany may not necessarily have mappedBy attribute. For instance, a product can have a list of AddOn items. Product and AddOn are @ManyTo@Many relationship. An AddOn can be in many product, not particular product. So it doesn't have property "product". It can't do mappedBy unless we add another entity ProductAddOn which adds unnecessary complexity. However, without mappedBy, I can't use @AdminPresentationCollection. It throws exception.

Any workaround other than adding extra entity like ProductAddOn?

Thank you!

-Charlie

Re: PresentationCollection for ManyToMany without mappedBy

Posted: Sun Mar 23, 2014 7:23 pm
by phillipuniverse
You could try using the 'manyToField' property on @AdminPresentationCollection:

Code: Select all

/**
 * <p>Optional - only required in the absence of a "mappedBy" property
 * on the JPA annotation</p>
 *
 * <p>For the target entity of this collection, specify the field
 * name that refers back to the parent entity.</p>
 *
 * <p>For collection definitions that use the "mappedBy" property
 * of the @OneToMany and @ManyToMany annotations, this value
 * can be safely ignored as the system will be able to infer
 * the proper value from this.</p>
 *
 * @return the parent entity referring field name
 */
String manyToField() default "";
 


Let me know if that works. If it doesn't, I would open an issue on https://github.com/BroadleafCommerce/BroadleafCommerce because that's exactly what that functionality was designed to do.

Re: PresentationCollection for ManyToMany without mappedBy

Posted: Tue Mar 25, 2014 3:50 pm
by pokemon007
This won't work. It's essentially the same as mappedBy, and only for those that don't use mappedBy annotation. Essentially it needs a child object's property that can be referenced to get to parent object. In the case I mentioned the child objects do not have a reference to parent object. Following is the relationship between product and addOn:

Code: Select all

    @ManyToMany(fetch = FetchType.LAZY, targetEntity = AddOn.class)
    @JoinTable(name = "PRODUCT_ADD_ON", joinColumns = @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "PRODUCT_ID"), inverseJoinColumns = @JoinColumn(name = "ADD_ON_ID", referencedColumnName = "ADD_ON_ID"))
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="blStandardElements")
    @BatchSize(size = 50)
     protected List<AddOn> addOns= new ArrayList<AddOn>();


The reference from addOn to product is through hibernate @JointTable's attributes. AddOn itself doesn't know Product.

The workaround I mentioned in my original post works well. So I may just keep it for now.

Thanks.

-Charlie