문제

Hi I am using Liferay SKD for Java and Liferay 6.1(Tomcat). I have created custom database as follows:

<entity name="PCustomer" local-service="true" remote-service="false">

    <!-- PK fields -->
    <column name="customerId"           type="int" primary="true" />
    <!-- Audit fields -->
    <column name="name"                 type="String"/> 
    <column name="vAddress"             type="String"/>
    <column name="pAddress"             type="String"/>
    <column name="comments"             type="String"/>
    <column name="pusers"               type="Collection"       entity="PUser"      mapping-key="userId"/>
    <column name="pcontacts"            type="Collection"       entity="PContact"   mapping-key="contactId"/>
    <column name="pdemos"               type="Collection"       entity="PUserDemo"  mapping-key="demoId"/>
    <column name="plicenses"            type="Collection"       entity="PLicense"   mapping-key="licenseId"/>
    <column name="pfolders"             type="Collection"       entity="PFolder"    mapping-key="folderId"/>

</entity>

Using Service.xml and now I want to retrieve all of my contacts associated with certain customer. The problem is that when I do this in my JSP page:

<%
    String user = request.getRemoteUser();
    int userId = Integer.valueOf(user);
    PUser pUser=PUserLocalServiceUtil.getPUser(userId);
    int customerId = pUser.getCustomerId();

     PCustomer customer=PCustomerLocalServiceUtil.getPCustomer(customerId);

     java.util.List<PContact> contCus=PCustomerUtil.getPContacts(customerId);
%>

And try to go trough this list using for each loop:

%for (PContact pContact : contCus) 
    if(pContact.getUserType().equals("billing"))
    {%> DO SOMETHING <% } %>

It gives me an error:

java.lang.ClassCastException: $Proxy288 cannot be cast to com.myportlet.service.persistence.PCustomerPersistence

I debugged it and all of the values are OK and its working until it try to make the list in the JSP page. The thing is that in the page it is showing me that I have to construct the list like that. Using this parameters and so on. It doesn't give me any errors.

Can someone help me or tell me what am I doing wrong?

Any help will be appreciated. Thanks in advance!!!!

도움이 되었습니까?

해결책 2

I think this has nothing to do with Java collections nor iteration ...

It seems that you are trying to cast a Spring managed bean to a concrete class somewhere in your code. The problem is that Spring AOP uses proxies to wrap around beans, and in this circumstances, the only thing you can assume is that the proxies implement the same set of interfaces as the original class. The weird thing is that, as you're using service builder to generate your services, PCustomerPersistence should already be an interface and not a class.

Btw, you should never call PCustomerUtil (or the equivalent class for other entities generated by service builder) from outside your services. Those classes expose methods directly from the persistence layer, and so, should be called only from your services, not from JSPs or portlets.

다른 팁

Try to use JSTL c:forEach tag:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% pageContext.setAttribute("contCus", contCus); %>

<c:forEach var="pContact" items="${contCus}">
    <c:out value="${pContact.userType}"/>
</c:forEach>    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top