简体   繁体   中英

while retrieving entity occurs an error

When I retrieve the object from datastore, I am getting this error:

com.google.appengine.datanucleus.query.StreamingQueryResult cannot be cast to com.relationship.Student`

This is my code:

javax.jdo.Query q=pm.newQuery(Student.class);

//String fname="karthi";
q.setFilter("name == 'karthi'");
@SuppressWarnings("unchecked")
List<Employee> results = (List<Employee>) q.execute("karthi@gmail.com");
Student e = (Student) results;
resp.getWriter().println(e.getName());  

The datastore has those properties and values:

ID/Name            college       name
karthi@gmail.com   AMS           karthi

You're casting an object of type List<Employee> to an object of type Student . Unless you explicitly created a constructor for Student that takes a List<Employee> as its argument, this won't work.

The fact that you added @SuppressWarnings("unchecked") tells me that your compiler/IDE already warned you about this, and instead of attempting to understand why it was complaining, you decided the quick fix would be to basically ignore the problem by misusing this annotation.

Now the problem is unavoidable: why are you casting a List<Employee> to Student ? No part of that makes sense. Does your code actually allow this? How does a student need a list of employees?

I think you need to do some more basic study in java to understand what an object is, and what it means for an object to have a type, or class, that it's an instance of. Sorry I couldn't be of more help. Keep learning java and hopefully you can understand why this error occurs.

Also, in future, you should include all relevant information for a user to help you. I would normally request you to post, for example, the code that instantiated q , in addition to the full stack trace rather than just one line from the stack trace.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM