Page 1 of 1

Filter Mapping for OneToMany Relationship Entity

Posted: Wed Jul 09, 2014 9:26 am
by retaildev
Hi All,

I have a functionality to build here and i need to know what could be the right way for doing this.

We have customer entity. I need to extend this functionality a bit. We are implementing multichannel approach for this a customer can be created through multiple channel. So there is a channelmaster entity and than there is a join table holding the customer id and channelid. In db terms this can be viewed as a manytomany relationship.

Now i need to build this in admin to filter out customers based on particular channels. I am putting a list of channels in customerimpl, now i have to build this filtermapping. I need to build a generic approach for this instead of going through named queries or detach criteria.

What i have understood that there are different persistence modules (basic, adorned etc) currently customerimpl falls in basic persistance module category. Now i have to put a criteria in persistence perspective so that correct filter mappings are created for it. Not sure how to achieve this and which one to chose.

Kindly help me out on this feature, looking for guidance.

Thanks
Dev

Re: Filter Mapping for OneToMany Relationship Entity

Posted: Thu Jul 10, 2014 3:31 pm
by retaildev
Hi All,

I have gone a bit ahead in this problem and got to this criteria hibernate.
This code is from CriteriaTranslatorImpl.

Class<Serializable> ceilingClass = determineRoot(dynamicEntityDao, ceilingMarker, filterMappings);
CriteriaQuery<Serializable> criteria = criteriaBuilder.createQuery(ceilingMarker);
Root<Serializable> original = criteria.from(ceilingClass);
criteria.select(original);

Join<Serializable, Serializable> tags = original.join("channel");
Predicate predicate = criteriaBuilder.equal(tags.get("id"),1);
criteria.where(predicate);

Now this works and generates a hibernate query like this

select customerim0_.CUSTOMER_ID as CUSTOMER1_34_, customerim0_.USER_NAME as USER16_34_ .....
from BLC_CUSTOMER customerim0_
inner join ez_customer_channel channel1_ on customerim0_.CUSTOMER_ID=channel1_.CUSTOMER_ID
inner join BLC_Channel channelimpl2_ on channel1_.channel_ID=channelimpl2_.Channel_ID
where channelimpl2_.channel_ID=51

This is working fine. Now i need to have a generic approach by setting up this detail in PersistencePerspective through FilterAndSortCriteria and then creating filtermapping through it and than restrictions from it.

I am not able to get to the correct way of putting this in FilterAndSort and than it is there in restrictions predicate and used in query in CriteriaTranslater

Thanks
Dev

Re: Filter Mapping for ManyToMany Relationship Entity

Posted: Tue Jul 15, 2014 9:41 am
by retaildev
Hi All,

Any help on this will be really appreciated...

Thanks
Kunal

Re: Filter Mapping for OneToMany Relationship Entity

Posted: Thu Jul 17, 2014 11:38 am
by phillipuniverse
@retaildev.

Assuming that you have this situation:

Code: Select all

public class MyCustomer extends CustomerImpl {
    
    @
ManyToMany
    
List<ChannelMasterchannelMasters;
}
 


You should be able to do this:

Code: Select all

public class MyCustomer extends CustomerImpl {
    
    @
ManyToMany
    
@AdminPresentationCollection(manyToField "customers")
    List<
ChannelMasterchannelMasters;
}

public class 
ChannelMaster {
    @
ManyToMany
    
@AdminPresentationCollection(manyToField "channelMasters")
    List<
Customercustomers;
}
 


I think that you have to get the reverse relationship in order for it to work correctly. That also assumes that you are trying to show a list of ChannelMasters for a particular customer, or vice-versa.