0

I have a problem while reading value of a List. I have this block of code:

            Query<User> qr=ds.createQuery(User.class).field("UserID").equal(52005);
            List<User> l=qr.asList();
            Iterator i=l.iterator();
            System.out.println(i.next().toString());

I have a list l this list is populated with a query on a database (return a record with the UserID 52005). If I print the size of l I can see the correct value (1) because there is a UserID with the value of 52005, but my problem is now I cant read the content of this list. If I use the above code I see something else what I expect i.e Untersuchungsraum.User@88c615

It seems that it is encoded, Untersuchungsraum is the name of the package and User is the name of my POJO class.

Here is my Pojo:

@Entity("Users")
public class User {
    @Id private String id;
    private String City;
    private int  UserID; 

    /**
     * @return the city
     */
    public String getCity() {
        return City;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.City = City;
    }
...

How can I access to value of my list in this sitaution How can I see the City value in the list l

4

3 回答 3

6

问题不在于列表或迭代器,而在于您如何处理User. 当你运行时:

 System.out.println(i.next().toString());

它调用User#toString()的是未被覆盖的,所以它实际上调用了Object#toString. 这给了你那个东西。利用:

 System.out.println(i.next().getCity());
于 2013-09-05T13:46:39.400 回答
0
        Query<User> qr=ds.createQuery(User.class).field("UserID").equal(52005);
        List<User> l=qr.asList();
        if(l!=null && l.size()>0){
             User user =  l.get(0);
             System.out.println(user.getCity());
             System.out.println(user.getUserID());

        }
于 2013-09-05T13:48:31.697 回答
0

所有你需要的是list.get(0).getCity()

于 2013-09-05T13:46:17.990 回答