Question

In my App Engine Connected Android Project i want to fetch 2 column from Department Entity. Department has 3 property. 1.Id
2.Description
3.Member

I want to fetch fetch only Id and Description column.This is my code:

Department Entity:
        @Entity
        public class Department {
              @Id
              private String id;

              private String description;

              @OneToMany(mappedBy = "department", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
              private List<Manager> manager = new ArrayList<Manager>();

                  //getter and setter Method
        }

Endpoint Method:

@ApiMethod(name = "listParent",path = "get_listParent")
public CollectionResponse<Department> listParent(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit,
        @Nullable @Named("fetch") String fetch) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Department> execute1 = null;
    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery("select description, 
                                id from Department as Department where id = '"+fetch+"'");
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }
        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }
        execute1 = (List<Department>) query.getResultList();            
        for (Department obj : execute1){
            LOGGER.severe("Description:"+obj.getDescription());
        }

    } finally {
        mgr.close();
    }

  return CollectionResponse.<Department> builder().setItems(execute1)
            .setNextPageToken(cursorString).build();
}

so the obj.getDescription() throws "cannot be cast to com.datanucleus.Department java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.datanucleus.Department" Exception.

How Can i Solve This Problem.

Thanks In Advance

Update: in the above Code i solve the java.lang.ClassCastException using following code:

  execute = (List<Object[]>) query.getResultList();     
  Object[] firstRow = execute.get(0);
  LOGGER.severe("List ID"+firstRow[0]);
  LOGGER.severe("List Description"+firstRow[1]);

now my problem is how to send firstRow array To Android Client using Endpoint.?

Was it helpful?

Solution

I suggest that you determine what is the object that you want to pass from the Endpoint implementation back to the Android client.

In your case, you want the id and the description only for the Department entity. Though it would be some additional work, I would recommend that you define separate Data Transfer Objects that are separate from your actual Entity objects. It will lend itself well to future enhancements/changes and promote loose coupling.

So you could define another Java class say DepartmentDTO or something like that and in that class simply have the id and description fields only. You can then change the Endpoint class signature to use the DepartmentDTO instead of the Department entity.

OTHER TIPS

You select 2 fields in your query, and seem to think that means the result is a Department, which it clearly isn't since you selected just those 2 fields. The result in that case is an Object array.

If you want a full Department then you do

SELECT d FROM Department d ...

As per any JPA docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top