有关 JSR-303、Tapestry 和 JPA 实体继承的复杂用例

发布于 2024-12-10 04:55:46 字数 2508 浏览 1 评论 0原文

我有一个名为 ParentAccount 的 JPA 实体,它扩展了一个抽象 Account 实体(请参阅 JPA 继承)。我已将 JSR-303 验证约束放置在 Account 实体中。 现在我有以下 Tapestry 类和模板,并且 JSR-303 验证似乎不起作用:

Tapestry 类:

 public class Inscription {        
    @Property
    //this is not validated...
    private ParentAccount parentAccount;

    @Property
    @Validate("required")
    private String accountPasswordConfirmation;    

    @InjectComponent
    private Form registrationForm;

    @OnEvent(EventConstants.PREPARE)
    void prepareAccount(){
        parentAccount = new ParentAccount();
    }

    @OnEvent(value= EventConstants.VALIDATE)
    void validateRegistrationForm() {
        if(registrationForm.isValid()) {
            if(accountPasswordConfirmation.equals(parentAccount.getAccountPassword())) {
                System.out.println("ok for insert");
            }
        }
    }
}

Tapestry 页面:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<head>
    <title>Hello World Page</title>
</head>
<body>
<form t:type="form" t:id="registrationForm" validate="this">
    <t:errors/>
    <div>
        <label t:type="label" for="accountEmailAddress"/>
        <input t:type="textfield" t:id="accountEmailAddress" value="parentAccount.accountEmailAddress"/>
    </div>
    <div>
        <label t:type="label" for="accountFirstName"/>
        <input t:type="textfield" t:id="accountFirstName" value="parentAccount.accountFirstName"/>
    </div>
    <div>
        <label t:type="label" for="accountLastName"/>
        <input t:type="textfield" t:id="accountLastName" value="parentAccount.accountLastName"/>
    </div>
    <div>
        <label t:type="label" for="accountPassword"/>
        <input t:type="textfield" t:id="accountPassword" value="parentAccount.accountPassword"/>
    </div>
    <div>
        <label t:type="label" for="accountPasswordConfirmation"/>
        <input t:type="textfield" t:id="accountPasswordConfirmation" value="accountPasswordConfirmation"/>
    </div>
    <div><input type="submit" value="ok"/></div>
</form>
</body>
</html>

不幸的是,即使我使用 @NotNull 注释对实体进行了注释,那些 JSR- 303 约束被忽略。

有人可以帮忙吗?

问候,

I have a JPA entity called ParentAccount that extends an abstract Account entity (see JPA inheritance). I have place the JSR-303 validation constraints in the Account entity.
Now I have the following Tapestry class and templates and JSR-303 validation does not seem to work:

Tapestry class:

 public class Inscription {        
    @Property
    //this is not validated...
    private ParentAccount parentAccount;

    @Property
    @Validate("required")
    private String accountPasswordConfirmation;    

    @InjectComponent
    private Form registrationForm;

    @OnEvent(EventConstants.PREPARE)
    void prepareAccount(){
        parentAccount = new ParentAccount();
    }

    @OnEvent(value= EventConstants.VALIDATE)
    void validateRegistrationForm() {
        if(registrationForm.isValid()) {
            if(accountPasswordConfirmation.equals(parentAccount.getAccountPassword())) {
                System.out.println("ok for insert");
            }
        }
    }
}

Tapestry page:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<head>
    <title>Hello World Page</title>
</head>
<body>
<form t:type="form" t:id="registrationForm" validate="this">
    <t:errors/>
    <div>
        <label t:type="label" for="accountEmailAddress"/>
        <input t:type="textfield" t:id="accountEmailAddress" value="parentAccount.accountEmailAddress"/>
    </div>
    <div>
        <label t:type="label" for="accountFirstName"/>
        <input t:type="textfield" t:id="accountFirstName" value="parentAccount.accountFirstName"/>
    </div>
    <div>
        <label t:type="label" for="accountLastName"/>
        <input t:type="textfield" t:id="accountLastName" value="parentAccount.accountLastName"/>
    </div>
    <div>
        <label t:type="label" for="accountPassword"/>
        <input t:type="textfield" t:id="accountPassword" value="parentAccount.accountPassword"/>
    </div>
    <div>
        <label t:type="label" for="accountPasswordConfirmation"/>
        <input t:type="textfield" t:id="accountPasswordConfirmation" value="accountPasswordConfirmation"/>
    </div>
    <div><input type="submit" value="ok"/></div>
</form>
</body>
</html>

Unfortunately, even though I have annotated the entity with @NotNull annotations, those JSR-303 constraints are ignored.

Can anyone please help?

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-12-17 04:55:46

我注意到,如果我向 Tapestry 类中的属性添加 JSR-303 注释,它确实会被考虑在内。那么为什么 Tapestry 在放置在 JPA 实体的属性上时不会考虑注释?

在 Tapestry 中,表单由 ValidationTracker 关联它跟踪表单中每个字段的所有提供的用户输入和验证错误。

看来默认的 ValidationTrackerImpl 并不关心。您可以通过表单的跟踪器参数提供跟踪器实现。我做了一些与 Struts2 类似的事情来验证我的实体。

也许(希望)使用 @Valid (JSR-303 规范中所称的图形验证)注释您的 parentAccount 也有效(前提是 Tapestry 关心)。

I noticed that if I add a JSR-303 annotation to a property in the Tapestry class, it does get taken into account. Why then Tapestry won't take into account the annotation when placed upon a property of a JPA entity??

In Tapestry a Form is accociated by a ValidationTracker that tracks all the provided user input and validation errors for every field in the form.

It seems the default ValidationTrackerImpl doesn't care. You could provide your on tracker implementation via the Form's tracker parameter. I did something similiar with Struts2 to have my entities validated.

Maybe (hopefully) annotating your parentAccount with @Valid (graph validation as it is called in the JSR-303 specification) also works (provided Tapestry cares).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文