liferay-6classcastexceptioncomposite-keyliferay-service-builder

Class cast exception while using composite key in service builder


I have a parent portlet which uses Liferay service builder for persistence. The child portlets uses the same entities for persistence. So I have added the parent portlet as dependency in child portlet using Liferay plugin properties. Everything is fine if I have single primary key for entities. But in case of composite primary key entity, if I find an entity using it, class cast exception occurs. I couldn't find the reason. is it because of class loader issues?


Solution

  • The service jar exists in both parent portlet and child portlet which we can't avoid in this scenario. While debugging, even though the PK class object is created in child portlet, the persistence is happening in parent portlet using parent portlet class loader. This makes the issue.

    Solution is to create a method in localserviceImpl for persistence or fetching without using composite PK. Create with individual PK attributes. Create PK in localServiceImpl using the attributes and go for fetch or persistence.