Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 524
baudo2048 Avatar asked Jan 31 '17 14:01

baudo2048


People also ask

What does save method do in JPA?

save(…) -Method. It will persist or merge the given entity using the underlying JPA EntityManager . If the entity has not been persisted yet Spring Data JPA will save the entity via a call to the entityManager.

What is extends JpaRepository?

JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.

Which of the following interface repository interface has to extend in Spring data?

Typically, your repository interface will extend Repository , CrudRepository or PagingAndSortingRepository . Alternatively, if you do not want to extend Spring Data interfaces, you can also annotate your repository interface with @RepositoryDefinition .

What is the return type of save method in JPA repository?

JPA's persist method returns void and Hibernate's save method returns the primary key of the entity.


1 Answers

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.

like image 196
RealSkeptic Avatar answered Nov 01 '22 08:11

RealSkeptic