What does it mean <S extends T> save (S entity); in Spring Repository?

baudo2048 :

In Spring Data project the CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.

public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {

    <S extends T> S save(S entity);

    T findOne(ID primaryKey);

    Iterable<T> findAll();

    Long count();

    void delete(T entity);

    boolean exists(ID primaryKey);

    // … more functionality omitted.
}

In general, I know what "S extends T" means, i.e. that S, the return type of save operation, must be subtype of T. Why is it necessary to add such as constraint? I think that would be fine doing something like this:

T save (T entity);

As in

void delete(T entity);

I've googled to find more help and I've figured out following question on stackoverflow itself but it isn't really clear for me:

Spring CrudRepository: why to invent a new generic type S

thank you.

RealSkeptic :

If you were to have it as

T save (T entity);

Then the only variable you could assign the result to would have to be of type T.

So, if you have a CrudRepository<Animal,AnimalID> repository, and you have

Dog dog = getDog();
Dog savedDog = repository.save(dog);

You'd get a compile error - you can't assign the result to Dog, as it has to be of type T, in this case, Animal.

You'd need to check if the returned value was indeed of type Dog, and if so, cast it to Dog to put it in savedDog.

With the declaration as it is, it means that you can assign it to a variable of the same type as the original argument, as type resolution will allow that.

The declaration itself doesn't specify how the non-animal parts of the dog are saved if at all. All it does is allow assigning the result back to a Dog if it was originally a Dog.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=456357&siteId=1