Answer the question
In order to leave comments, you need to log in
How to get rid of TypeMismatchException (String->Long)?
Hello everyone,
Yes, I have a test
@Test
public void FindExists() throws Exception {
assertNotNull(userService.findById(1L));
}
public User findById(Long id) {
return userRepository.findOne(id);
}
org.hibernate.TypeMismatchException: Provided id of the wrong type for class cards.server.entity.User. Expected: class java.lang.String, got class java.lang.Long
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@NoRepositoryBean
public interface BaseRepository<T, Long extends Serializable> extends Repository<T, Long> {
void delete(T deleted);
List<T> findAll();
T findOne(Long id);
T save(T persisted);
}
@Repository
public interface UserRepository extends BaseRepository<User, Long> {
List<User> findByUsernameLike(String username);
User findByUsername(String username);
List<User> findByEnabledTrue();
List<User> findByEnabledFalse();
void deleteByUsername(String username);
}
public interface BaseService<T> {
T add(T item);
T update(Long id, T data);
void delete(Long id);
T findById(Long id);
List<T> findAll();
}
@Service
public class UserService implements BaseService<User> {
@Resource
private UserRepository userRepository;
public User add(User item) {
return userRepository.save(item);
}
public User update(Long id, User data) {
User user = findById(id);
user.setUsername(data.getUsername());
user.setDetails(data.getDetails());
user.setEnabled(user.getEnabled());
user.setPassword(data.getPassword());
user.setRole(data.getRole());
user.setPacks(data.getPacks());
return user;
}
public void delete(Long id) {
userRepository.delete(userRepository.findOne(id));
}
public User findById(Long id) {
return userRepository.findOne(id);
}
public List<User> findByUsernameLike(String username) {
return userRepository.findByUsernameLike(username);
}
public User findByUsername(String username) {
return userRepository.findByUsername(username);
}
public List<User> findAll() {
return userRepository.findAll();
}
}
@Resource
private UserService userService;
// some code
@Test(expected = NullPointerException.class)
public void FindDoesntExists() throws Exception {
assertNull(userService.findById(1000L));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question