hibernatesaprfc

Hibersap returning HashMaps from ArrayList instead of desired Objects


I'm working with a back end application and it uses Hibersap 1.2.0 to map SAP logic into our service.

I'm having a problem when accessing elements inside an ArrayList, here is my class:

import org.hibersap.annotations.BapiStructure;
import org.hibersap.annotations.Parameter;
import org.hibersap.annotations.ParameterType;
import org.hibersap.annotations.Table;

import java.util.List;

@BapiStructure
public class RFC_BP_Export {

    @Parameter("PARTNER")
    private String partner;

    @Parameter("NOMBREC")
    private String nombreC;

    @Parameter("DIRECCION")
    private String direccion;

    @Parameter("SMTP_ADDR")
    private String smtpAddr;

    @Parameter("TEL_NUMBER")
    private String telNumber;

    @Table
    @Parameter(value = "PERSONA_CONTACTO", type = ParameterType.STRUCTURE)
    private List<RFC_PERSONA_CONTACTO_Export> rfcPersonaContactoExportList;

    ... Getter and Setters...

    public void setRfcPersonaContactoExportList(List<RFC_PERSONA_CONTACTO_Export> rfcPersonaContactoExportList) {
        this.rfcPersonaContactoExportList = rfcPersonaContactoExportList;
    }

    @Override
    public String toString() {
        return "RFC_BP_Export{" +
                "partner='" + partner + '\'' +
                ", nombreC='" + nombreC + '\'' +
                ", direccion='" + direccion + '\'' +
                ", smtpAddr='" + smtpAddr + '\'' +
                ", telNumber='" + telNumber + '\'' +
                ", rfcPersonaContactoExportList=" + rfcPersonaContactoExportList +
                '}';
    }
}

The list rfcPersonaContactoExportList should contain an ArrayList of the following class:

import org.hibersap.annotations.BapiStructure;
import org.hibersap.annotations.Parameter;

    @BapiStructure
    public class RFC_PERSONA_CONTACTO_Export{

        @Parameter("PARTNER")
        private String partner;

        @Parameter("NOMBREC")
        private String nombreC;

        @Parameter("DIRECCION")
        private String direccion;

        @Parameter("SMTP_ADDR")
        private String smtpAddr;

        @Parameter("TEL_NUMBER")
        private String telNumber;

        @Parameter("XDFREL")
        private String xdfRel;


        ....Getters and Setters...

        @Override
        public String toString() {
            return "RFC_PERSONA_CONTACTO_Export{" +
                    "partner='" + partner + '\'' +
                    ", nombreC='" + nombreC + '\'' +
                    ", direccion='" + direccion + '\'' +
                    ", smtpAddr='" + smtpAddr + '\'' +
                    ", telNumber='" + telNumber + '\'' +
                    ", xdfRel='" + xdfRel + '\'' +
                    '}';
        }
    }

But when I try to get an element from the List, it returns a HashMap object instead of a RFC_PERSONA_CONTACTO_Export object. For example:

for(RFC_PERSONA_CONTACTO_Export contacto_export : bpExport.getRfcPersonaContactoExportList()){
     System.out.println("contacto " + bpExport);
}

Throws an Exception that says HashMap cannot be converted to RFC_PERSONA_CONTACTO_Export.

Am I missing an annotation in my classes? Why can't I access to the Object I want?


Solution

  • To workaround this issue, I had to use this code:

                Object contactoExportObj = bsExport.getRfcPersonaContactoExportList().get(i);
                HashMap<String, String> contactoHashMap = null;
                RFC_PERSONA_CONTACTO_Export rfcPersonaContact = new RFC_PERSONA_CONTACTO_Export();
                if(contactoExportObj instanceof HashMap){
                    contactoHashMap = (HashMap<String, String>) contactoExportObj;
                    rfcPersonaContact.setPartner(contactoHashMap.get("PARTNER"));
                    rfcPersonaContact.setDireccion(contactoHashMap.get("DIRECCION"));
                    rfcPersonaContact.setNombreC(contactoHashMap.get("NOMBREC"));
                    rfcPersonaContact.setSmtpAddr(contactoHashMap.get("SMTP_ADDR"));
                    rfcPersonaContact.setTelNumber(contactoHashMap.get("TEL_NUMBER"));
                    rfcPersonaContact.setXdfRel(contactoHashMap.get("XDFREL"));
    
                    contactManager.syncContact(rfcPersonaContact);
                    accountContactManager.syncAccountContact(bsExport.getPartner(), rfcPersonaContact.getPartner(), isPrimary(rfcPersonaContact.getXdfRel()));
                }
    

    As you can see, I'm assigning the element in the list to an Object instance, then create a HashMap variable and since at compilation time casting the contactoExportObj would throw an error, I used an if statement to check at runtime the instance I was getting, then I proceeded to pass the info to my desired object.

    I know this is not an ideal solution, the git of Hibersap had a branched solution but we're unable to use the dependencies that it uses due to project constraints.