I use Spring Data MongoDb for saving documents in database.
@Repository
public interface UserRepository extends MongoRepository<User, ObjectId> {
Optional<User> findByUsername(String userName);
}
@Document(collection = "users")
public class User {
@Id
private ObjectId id;
@Indexed(unique = true)
private String username;
...
@Service
public class UserUpdateServiceImpl implements UserUpdateService {
private UserRepository repository;
private UserMapper mapper;
@Autowired
public UserUpdateServiceImpl(UserRepository repository, UserMapper mapper) {
this.repository = repository;
this.mapper = mapper;
}
@Override
public void updateDocumentInCollection(UserDto userDto) {
User user = mapper.toEntity(userDto);
repository.save(user);
}
}
org.springframework.dao.IncorrectResultSizeDataAccessException: Query { "$java" : Query: { "username" : "user"}, Fields: {}, Sort: {} } returned non unique result.
Update_2
I used the operation insert
/**
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
* returned instance for further operations as the save operation might have changed the entity instance completely.
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
*
* @param entity must not be {@literal null}.
* @return the saved entity
* @since 1.7
*/
<S extends T> S insert(S entity);
public void updateUser(UserDto userDto) {
User user = mapper.toEntity(userDto);
userRepository.insert(user);
}
It again cause a mistake.
org.springframework.dao.DuplicateKeyException: E11000 duplicate key error collection: mongo-spring.users index: id dup key: { _id: ObjectId('5f3b786085c7d32787990955') }; nested exception is com.mongodb.MongoWriteException: E11000 duplicate key error collection: mongo-spring.users index: id dup key: { _id: ObjectId('5f3b786085c7d32787990955') }
Update_3
I run unit test.
@Test
void updateUser() {
UserDto user = readService.getDataOfUserByName("user");
user.setEnabled(true);
user.setAccountNonLocked(true);
List<Role> authorities = user.getAuthorities();
authorities.add(Role.ADMIN);
user.setAuthorities(authorities);
if(user.getUsername() != null)
userUpdateService.updateUser(user);
}
@Override
public void updateUser(UserDto userDto) {
User user = mapper.toEntity(userDto);
userRepository.save(user);
}
- mapper
public interface CommonMapper<D, E>{
D toDto(E e);
E toEntity(D d);
Iterable<D> toListDto(Iterable<E> entityList);
Iterable<E> toListEntity(Iterable<D> dtoList);
}
@Mapper(componentModel = "spring")
public interface UserMapper extends CommonMapper<UserDto, User> {
}
Now It work.
when I try to update a record, I get an exception...