java - 如何在表单绑定(bind)期间使用 Spring 的 IdToEntityConverter 进行实体类型转换?

标签 java spring jpa spring-mvc

我厌倦了为系统中的每个实体编写扩展 PropertyEditorSupport。

我注意到 Spring 3.0 有一个 IdToEntityConverter,但是关于如何使用它的文档真的不多。

请评论在 id 和实体之间转换的最佳通用方法。

最佳答案

IdToEntityConverter 是一个内部 Spring 类。它是非公开的,这就是它没有出现在 javadoc 中的原因,但它在每个上下文中都默认注册。类评论说:

Converts an entity identifier to a entity reference by calling a static finder method on the target entity type.

For this converter to match, the finder method must be public, static, have the signature find[EntityName]([IdType]), and return an instance of the desired entity type.

因此,如果您尝试绑定(bind)实体类 X,则 X 必须具有 public static findX(id) 或其他内容类似的,在 X 类上。

关于java - 如何在表单绑定(bind)期间使用 Spring 的 IdToEntityConverter 进行实体类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324034/

相关文章:

java - 接受 0.5 和整数的正则表达式

java - 如何处理@AttributeOverrides上的@Type

java - 为什么要在javabean类中提供公共(public)构造函数

java - 当值在 map 中时,Map.containsValue() 返回 false

java - 如何计算同一类的两个对象?

java - ManyToMany 和 OneToOne 关系的 Jpa 标准

java - JPA:无状态 session Bean 和托管实体

java - 为什么当 ftp java dsl 配置被引入 spring 集成核心时过滤器行为发生了变化

java - Bloch 的 Spring 构建器模式

java - 如何更改 Spring 5 webflux websocket 客户端上的帧/缓冲区大小