0

I have an EJB module with a remote interface, and an Enterprise Application Client (with a library that includes all the remote EJB's interfaces). Everything works fine when I call any business method from the remote EJB in the client, except when the EJB has to return me an Array of objects.

So transfering a String works fine, BUT IT WOULD NOT WORK WHEN i TRY TO SEND A AN ARRAY OF STRINGS.

Any ideas on this??? Do I need to create a Serializable object like this?? or WHAT?:

public class ArrayStrings () implements Serializable{
         private String[] arrayS;
         ...
}

I don't know how can I pass an array in a remote ejb call. Any ideas?

Find bellow my code:

@Stateless
public class CategoryManagement extends AbstractFacade<Category> implements 
CategoryManagementRemote {

@PersistenceContext(unitName = "On-lineSupermarketEJBPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public CategoryManagement() {
    super(Category.class);
}

@Override
public Category findCategoryByName (String name){

    try{
    Category c= (Category)em.createNamedQuery("Category.findByName").setParameter("name", name).getSingleResult();
    for (Product p: c.getProductCollection()){
        System.out.println(p.getName());
    }
    return c;
    }catch(NullPointerException e){};
    return null;

}

@Override
public List<String> getProductCollectionGivenCategory (String name){
    Category c = findCategoryByName (name);
    List<String> list=null;
    for(Product p: c.getProductCollection()){
        list.add(p.getName());
    }
    return list;
}

}

Then my remote interface (that is in a jar library in the classpath of the client):

@Remote
public interface CategoryManagementRemote {
    Category findCategoryByName (String name);
    List<String> getProductCollectionGivenCategory (String name);
}

And finally I call a method from the CategoryManagementRemote interface to just print out the array of Strings in the client:

public static void main(String[] args){
  try{
     InitialContext c = new InitialContext(); 
     CategoryManagementRemote cm = (CategoryManagementRemote) 
     c.lookup("java:global/On-lineSupermarketEJB/CategoryManagement");
     List<String> listProducts= 
     cm.getProductCollectionGivenCategory("Diary");
     for (String p: listProducts){System.out.println(p);}
   catch(Exception e){};
}

BTW I DONT GET ANY ERROR (I guess the try/catch catches the exception when trying to read the list of Strings). But the List is not printed out. (I am not receiving the list of Strings in the client (when just sending 1 single String it works)

pquin92
  • 1
  • 2
  • Please show the actual error you get and the method declarations from the remote interface and the actual EJB – Steve C Apr 21 '17 at 01:43
  • Well, `getProductCollectionGivenCategory` appears to have a bug that will result in a NullPointerException... If you logged your caught exception instead of swallowing it you may have seen that. – Steve C Apr 21 '17 at 02:46
  • If you are using Eclipse link, try to look at this answer of mine, I have spent days in trying to find out what was wrong: http://stackoverflow.com/questions/36112224/jpa-entity-is-not-correctly-passed-from-ejb-to-presentation-tier/37249863#37249863 – Leonardo Apr 21 '17 at 07:49
  • yes it looks like the problem is the eclipselink weaving. How did you fix the static weaving @Leonardo ???? – pquin92 Apr 21 '17 at 09:40
  • In my case it was a non critical application, so I disabled weaving. For static weaving try to see if one of the case apply to you: https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving – Leonardo Apr 21 '17 at 09:58
  • I disabled weaving and it works now. Thank you @Leonardo – pquin92 Apr 21 '17 at 17:13

1 Answers1

0

Solved! In my case I disabled weaving and it works now

pquin92
  • 1
  • 2
  • I still remember the frustration of those days, trying to find why things were not working as expected ! One other suggestion is to use Hibernate as JPA implementation. – Leonardo Apr 21 '17 at 17:32