我有这样的实体
@Entity(name = "recipes")
public class Recipe {
// ...
@Getter
@Setter
private List<Category> categories;
// ...
}
我对 MultiSelectComboBox 的 Binder 实现
private final Binder<Recipe> binder = new Binder<>(Recipe.class);
private final MultiSelectComboBox<Category> categoryMultiselect = new MultiSelectComboBox<>("Categories");
// ...
private void initBinder() {
binder.forField(categoryMultiselect)
.asRequired("Please fill this field")
.withValidator(s -> !s.isEmpty(), "Please select at least one category")
.withValidator(s -> s.size() <= 5, "Please select up to 5 categories")
.bind(Recipe::getCategories, Recipe::setCategories);
}
抛出
Incompatible types: Set<Category> is not convertible to List<Category>
,因为 MultiSelectComboBox 返回 Set
并且 Recipe::getCategories
返回 List<Category>
。
有什么想法可以避免这种情况吗?
PS:我真的不想将我的
List<Category>
中的Set<Category>
更改为Recipe.class
。
是的,它总是需要一个
Set
,因为这是其签名的一部分。这
意味着要选择的项目对于选择来说是唯一的,并且它们
是无序的 - 两者都是 List
无法承诺的 - 是否
这实际上对于尚不清楚的用例很重要。
因此,要么切换到另一个组件,允许订购和 重复项——或者在 UI 数据中的
Set
和 List
之间采用
类或活页夹(确保您的类别是一个好公民)
a Set
(给它等于和哈希码)并处理你的订单
List
通过某种方式(例如,为类别添加优先级))。