<S 扩展 T> 是什么意思保存(S实体);在 Spring 存储库中?

2024-08-24Java开发问题
2

免费vpn 本文介绍了<S 扩展 T> 是什么意思保存(S实体);在 Spring 存储库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! vpn下载 free vpn

问题描述

在 免费vpn Spring Data 项目中,CrudRepository 为正在管理的实体类提供复杂的 CRUD 功能.

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> vpn下载 S save(S entity);

    T findOne(ID primaryKey);

    Iterable<T> findAll();

    Long count();

  vpn free  免费vpn  免费vpn下载 void delete(T vpn下载 entity);

    boolean exists(ID primaryKey);

    // … more functionality omitted.
}

一般来说,我知道S extends T"是什么意思,即save操作的返回类型S必须是T的子类型.为什么要加上这样的约束?我认为这样做会很好:

In general, I know what "S extends T" means, 免费vpn下载 i.e. that 免费vpn S, the vpn下载 return type of save 免费vpn下载 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);

void delete(T entity);

我在谷歌上搜索以寻求更多帮助,我发现了关于 免费vpn stackoverflow 本身的以下问题,但对我来说并不是很清楚:

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

Spring CrudRepository:为什么要发明一个新的泛型S型

谢谢.

推荐答案

如果你想拥有它

T save (T vpn下载 entity);

那么您可以将结果分配给的唯一变量必须是 T 类型.

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

所以,如果你有一个 CrudRepository<Animal,AnimalID>存储库,你有

So, if 免费vpn下载 vpn free you have a CrudRepository<Animal,AnimalID> repository, and you have

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

你会得到一个编译错误 - 你不能将结果分配给 Dog,因为它必须是 T 类型,在这种情况下,动物.

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

您需要检查返回的值是否确实是 Dog 类型,如果是,则将其转换为 vpn下载 Dog 以将其放入 savedDog.

You'd need to check vpn下载 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 免费vpn it is, it means that you can assign it to a variable 免费vpn of the same type as the original argument, as type resolution will allow free vpn that.

声明本身并没有具体说明如何保存狗的非动物部分(如果有的话).它所做的只是允许将结果分配回 Dog 如果它最初是 Dog.

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

这篇关于&lt;S 扩展 T&gt; 是什么意思保存(S实体);在 Spring vpn free 存储库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make 免费vpn下载 a report page direction free vpn to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 免费vpn vpn下载 Eclipse 项目中使用西里尔文 .properties 免费vpn 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is free vpn there any 免费vpn way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from vpn下载 DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13