是否可以配置 Play Framework 的 CRUD 模块以遵守 @Column(unique=true) 注释?

发布于 2024-11-09 06:03:56 字数 1036 浏览 0 评论 0原文

我正在使用 Play 的 CRUD 模块创建一组简单的管理屏幕。我的模型之一是用户,我想对电子邮件字段强制执行唯一约束。

代码如下所示:

public class User extends Model {
    @Email
    @Required
    @Column(unique=true)
    public String email;

管理屏幕正确显示 - 当我尝试打破唯一性(通过使用已使用的电子邮件保存用户)时,我收到此错误(在浏览器中):

Execution exception
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

In {module:crud}/app/controllers/CRUD.java (around line 100)

96:
             } catch (TemplateNotFoundException e) {

97:
                 render("CRUD/show.html", type, object);

98:
             }

99:
         }


100:
         <b>object._save();</b>

101:
         flash.success(Messages.get("crud.saved", type.modelName));

102:
         if (params.get("_save") != null) {

103:
             redirect(request.controller + ".list");

104:
         }

105:
         redirect(request.controller + ".show", object._key());

106:
     }

我可以进行任何调整来使用 CRUD模块和列唯一性注释?

I'm using Play's CRUD module to create a simple set of admin screens. One of my models is User and I want to enforce a unique constraint on the email field.

The code looks like this:

public class User extends Model {
    @Email
    @Required
    @Column(unique=true)
    public String email;

The admin screen displays correctly - when I try to break uniqueness (by saving a user with an already used email) I get this error (in the browser):

Execution exception
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

In {module:crud}/app/controllers/CRUD.java (around line 100)

96:
             } catch (TemplateNotFoundException e) {

97:
                 render("CRUD/show.html", type, object);

98:
             }

99:
         }


100:
         <b>object._save();</b>

101:
         flash.success(Messages.get("crud.saved", type.modelName));

102:
         if (params.get("_save") != null) {

103:
             redirect(request.controller + ".list");

104:
         }

105:
         redirect(request.controller + ".show", object._key());

106:
     }

Are there any tweaks I can make to use the CRUD module AND column uniqueness annotations?

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

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

发布评论

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

评论(2

背叛残局 2024-11-16 06:03:56

您可以创建自定义检查并将其添加到 User 类中的电子邮件属性中。

public class UniqueCheck extends Check {

    @Override
    public boolean isSatisfied(Object validatedObject, Object value) {
        if (StringUtils.isBlank((String) value)) {
            return false;
        }
        return User.findByEmail((String) value));
    }

}

然后

public class User extends Model {

@Email
@Required
@MaxSize(value = 250)
@CheckWith(value = UniqueEmail.class, message = "Existing account has been found with this e-mail")
public String email;

}

You can create a custom check and add to the email property in the User class.

public class UniqueCheck extends Check {

    @Override
    public boolean isSatisfied(Object validatedObject, Object value) {
        if (StringUtils.isBlank((String) value)) {
            return false;
        }
        return User.findByEmail((String) value));
    }

}

Then

public class User extends Model {

@Email
@Required
@MaxSize(value = 250)
@CheckWith(value = UniqueEmail.class, message = "Existing account has been found with this e-mail")
public String email;

}
悲念泪 2024-11-16 06:03:56

在我看来,系统正在按照您的预期进行操作。我唯一能想到的是修补 CRUD 模块以捕获此异常并变得更好,但通常唯一的约束是数据库端约束,只能通过尝试写入数据库来检查。因此,唯一约束不是验证注释(可以在写入数据库之前执行)。

It sounds to me like the system is doing just what you expected. The only thing I can imagine is patching the CRUD module to catch this exception and be nicer, but generally unique constraints are database-side constraints and can only be checked by trying to write to the database. So a unique constraint is not a validation annotation (which can be executed before writing to the database).

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