@Override public void saveOrUpdate(Collection<User> users) { if (users == null || users.size() == 0) { return; } HashMap<Long, User> uid2user = new HashMap<Long, User>(); Collection<Long> uids = new ArrayList<Long>(); for (User user : users) { uid2user.put(user.getUid(), user); uids.add(user.getUid()); } List<User> existsUsers = createQuery().filter("uid in", uids).asList(); HashSet<Long> existsUids = new HashSet<Long>(); List<User> newUsers = new ArrayList<User>(); for (User existsUser : existsUsers) { existsUids.add(existsUser.getUid()); User updateUser = uid2user.get(existsUser.getUid()); existsUser.setScreenName(updateUser.getScreenName()); existsUser.setName(updateUser.getName()); existsUser.setProfileImageUrl(updateUser.getProfileImageUrl()); existsUser.setFollowersSize(updateUser.getFollowersSize()); existsUser.setTweetsSize(updateUser.getTweetsSize()); existsUser.setDetail(updateUser.getDetail()); newUsers.add(existsUser); updateUser.setId(existsUser.getId()); } for (User user : users) { if (!existsUids.contains(user.getUid())) { newUsers.add(user); } } ds.save(newUsers); }
@Override public void saveOrUpdate(User user) { Query<User> q = createQuery().filter("uid", user.getUid()); User pUser = q.get(); if (pUser == null) save(user); else { UpdateOperations<User> uo = createUpdateOperations(); uo.set("screenName", user.getScreenName()); uo.set("name", user.getName()); uo.set("profileImageUrl", user.getProfileImageUrl()); uo.set("followersSize", user.getFollowersSize()); uo.set("tweetsSize", user.getTweetsSize()); uo.set("detail", user.getDetail()); update(q, uo); user.setId(pUser.getId()); } }