Spring CrudRepository:为什么要发明一个新的泛型类型S. [英] Spring CrudRepository: why to invent a new generic type S

查看:286
本文介绍了Spring CrudRepository:为什么要发明一个新的泛型类型S.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否给我一个提示为什么在save方法的CrudRepository接口中有一个新的泛型类型S被发明。 (请参阅 org.springframework .data.repository.CrudRepository



以下是对CrudRepository类的提取:

  public interface CrudRepository< T,ID extends Serializable> 
扩充知识库< T,ID> {
/ **
*保存给定的实体。使用返回的实例进行进一步的
*操作,因为保存操作可能已经完全更改了
*实体实例。
*
* @param entity
* @返回保存的实体
* /
< S扩展T> S保存(S实体);

...

/ **
*删除给定的实体。
*
* @param entity
* @throws在给定实体为(@literal null)的情况下,IllegalArgumentException。
* /
void delete(T entity);

$ / code>

请注意,在第二种方法删除中,同一个实体的类型为T 。
方法save表示它返回一个S类型的对象,所以这是不可能实现的方法保存说这样的:



<$ (S实体){
save(实体);
返回findOne(entity.getMyTypeId(entity));
返回findOne(entity.getMyTypeId )); // Type mismatch:can not convert from MyType to S
}

另一个问题是如何更好地实现save方法?

解决方案

通常,Spring * Repository 接口是由Spring Data自动实现的,而不是通过手工实现的,并且知道你会得到相同类型的o当你放入时,从 save >操作中退出。特别是对于面向文档的数据存储,看到子类与超类保持在同一个存储库并且使用 S扩展T 作为参数,并且返回类型确保您可以继续使用与保存之前相同接口的持久对象。


Could you please give me a hint why in CrudRepository interface in the "save" method there is a new generic type "S" is invented. (see org.springframework.data.repository.CrudRepository)

Here is an extraction for CrudRepository class:

public interface CrudRepository<T, ID extends Serializable>
         extends Repository<T, ID> {
    /**
     * Saves a given entity. Use the returned instance for further
     * operations as the save operation might have changed the
     * entity instance completely.
     * 
     * @param entity
     * @return the saved entity
     */
     <S extends T> S save(S entity);

     ...

     /**
      * Deletes a given entity.
      * 
      * @param entity
      * @throws IllegalArgumentException in case the given entity is (@literal null}.
      */
      void delete(T entity);
}

Please note, in the second method "delete" the same entity has type T. The method "save" says it returns an object of type "S". So this is impossible to implement the method "save" say like this:

@Override
public <S extends MyType> S save(S entity) {
    save(entity);
    return findOne(entity.getMyTypeId()); // Type mismatch: cannot convert from MyType to S
}

The other question is how to implement the method "save" better?

解决方案

The purpose is that generally, Spring *Repository interfaces are implemented automatically by Spring Data and not by hand, and that it's convenient to know that you'll get the same type of object back out of the save operation as you put in. Especially for document-oriented data stores, it's not unusual to see subclasses persisted in the same repository as superclasses, and using S extends T as the parameter and return type ensures that you can keep working with the persisted object with the same interface you would have before saving it.

这篇关于Spring CrudRepository:为什么要发明一个新的泛型类型S.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆