5

This is my code :

public interface UserRepo extends CrudRepository<User, Long> {

    boolean exist(Long id);

    @Override
    User save(User user);

}

In eclipse, there is a warning on the return type User.

Description Resource Path Location Type Type safety: The return type User for save(User) from the type UserRepo needs unchecked conversion to conform to S from the type CrudRepository UserRepo.java

May I know

  1. what is the reason ecplise warning on return type unchecked conversion?
  2. what is the correct way to get rid of the warning?

TQ

user3832050
  • 53
  • 1
  • 4
  • 9
    You can just remove the save method. It doesn't provide any advantage if you declare it like this, it is already defined in the CrudRepository. – dunni Jan 15 '17 at 00:38
  • 2
    Yes, you can remove the line and the problem, but it doesn't answer why such a warning is generated and how to solve it. That's something I'd like to understand too, to improve my knowledge of generics. And it can be useful to add in your repo interface the methods that are actually used in your program, as it provides a means to easily find all calls to these methods. – zog le heros Apr 12 '17 at 12:18

1 Answers1

3

As @dunny figured out in his comment, this statement makes no sense in the interface, as it is already implemented in org.springframework.data.repository.CrudRepository.save(S)

Eclipse gives this warning as it can not know, that the S in the super implementation is a User in this case.

In order to answer your 2. question, you can do a

@Override
<S extends User> S save(S user);

Then you get rid of the warning, but even then, it does not make more sense to provide this signature.

Just skip this statement, as it is already there.

davey
  • 1,666
  • 17
  • 24
  • 4
    One reason you may want to do this if to, say, override the default `@Transactional` behaviour of the save method, say, setting a non-default `propagation` – Ben M Nov 13 '17 at 16:56