gpt4 book ai didi

Java EE 参数约束配置奇怪的行为

转载 作者:行者123 更新时间:2023-12-01 14:23:37 24 4
gpt4 key购买 nike

我试图在标准 AbstractFacade(由 NetBeans 生成)的创建和编辑方法上添加参数约束配置(bean 验证)。

所以我试过:

@Override
public void create(@WkTeilnahmePlanedResult WkTeilnahme entity) {
super.create(entity);
}

这返回了消息

A method overriding another method must not alter the parameter constraint configuration when deploying it to Glassfish 4



所以下一次尝试是
  @Override
public void create(WkTeilnahme entity) {
checkedCreate(entity);
}


private void checkedCreate(@WkTeilnahmePlanedResult WkTeilnahme entity) {
super.create(entity);
}

部署没有任何问题......但从未调用 validator 。

你能告诉我为什么吗?

顺便提一句:
  @Override
public void create(WkTeilnahme entity) {
throw new UnsupportedOperationException(
"Create not supported! Use checkedCreate() instead!");
}


public void checkedCreate(@WkTeilnahmePlanedResult WkTeilnahme entity) {
super.create(entity);
}

这有效,但不是很酷!

最佳答案

关于您的第一次尝试,它不起作用,因为 Bean 验证约束必须遵循 Liskov Substitution Principle .另请参阅相关的 Bean 验证规范部分 - http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-methodlevelconstraints-inheritance

从规范:

Very informally speaking, the Liskov substitution principle says that where a given type T is used, it should be possible to replace T with a sub-type S of T ("Behavioral subtyping"). If S overrides/implements a method from T and S would strengthen the method's preconditions (e.g. by adding parameter constraints) this principle would be violated as client code working correctly against T might fail when working against S. Also if S overrides/implements a method from T and S weakens the method's postconditions this principle would be violated. However S may strengthen the method's postconditions (by adding return value constraints), as client code working against T still will work against S.



我认为你的第二个例子应该可以工作,但是,我不熟悉 NetBeans AbstractFacade。我的猜测是调用了 checkedCreate(entity);不通过代理实例,因此不会被拦截。也许您可以发布所涉及类的完整代码?什么类型的类包含这些方法? session bean?

关于Java EE 参数约束配置奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com