C
C
Cyrus Smith2016-02-02 05:46:24
Java
Cyrus Smith, 2016-02-02 05:46:24

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));
    }

1L - unit disguised as Long. Isn't it true?
findById() looks like this
public User findById(Long id) {
        return userRepository.findOne(id);
    }

Question: Why is Java yelling at me that it needs ... String ?!
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

Thanks
Update: Complete "id variable life cycle
In POJO Users.java
@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

in the repositories. BaseRepository.java
@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);
}

Its successor is UserRepository.java
@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);
}

Now services. BaseService.java
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();
}

Its successor is UserService.java
@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();
    }
}

Well, the UserRepository test itself
@Resource
    private UserService userService;
    // some code
    @Test(expected = NullPointerException.class)
    public void FindDoesntExists() throws Exception {
        assertNull(userService.findById(1000L));
    }

Project Structure
QD2nJ9s.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-02-02
@zolt85

Here hibrnate seems to hint to you that the id of the User entity is declared as a String.
Either change the id type to Long, or (a crutch quick option) do

public User findById(Long id) {
        return userRepository.findOne(Long.toString(id));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question