spring - Check if User object exists in @DBRef List<User> -
i'm making use of mongodb, spring data , spring mvc. have user model has list of contacts:
class user { @dbref private list<user> contacts = new arraylist<user>(); public list<user> getcontacts() { return contacts; } }
i have 4 users inside database. 1 user has particular contact (who referred same collection id).
now, want check whether user has particular contact. use following code:
user userloggedin = userservice.getloggedinuser(); //user object user contact = userservice.findbyid(contactid); //contact if(userloggedin.getcontacts().contains(contact)) { system.out.println("has contact."); }
this output message not shown. however, if print list of contacts of user , id's, see contact inserted inside list of user.
i noticed if print hashcode of contact object , 1 inside list, different value, assume though details same, object isn't.
how can approach problem checking whether inside list. or should compare id?
otherwise stated: how can check whether object exists in contacts list?
you should override equals method in user.
from javadoc:
boolean contains(object o)
returns true if list contains specified element. more formally, returns true if , if list contains @ least 1 element e such (o==null ? e==null : o.equals(e)).
with equals must override , hashcode
Comments
Post a Comment