【问题标题】:BeanValidation to check if delete is possibleBeanValidation 检查是否可以删除
【发布时间】:2013-07-23 06:45:24
【问题描述】:

如何在删除前使用 BeanValidation 验证记录?例如我只能在executeDate字段在未来时删除记录。

我在我的项目中使用了成功的 beanvalidation。它是基于注释的配置。我通过 Controller 方法上的 @Valid 注释来使用它。它是基于 Spring 的 MVC 应用程序。

我的环境:

<hibernate4.core.version>4.2.1.Final</hibernate4.core.version>
<spring.version>3.2.2.RELEASE</spring.version>
<hibernate.search.version>4.2.0.Final</hibernate.search.version>

编辑:
我看过这个:hibernate validator - different groups on create, update, delete,还有 Hibernate Documentation,但对我来说这不是钢铁。 如果到目前为止我有验证工作,我只需要添加删除的特殊情况吗?我很困惑只需要执行哪些步骤才能获得这一项功能。

【问题讨论】:

标签: spring hibernate spring-mvc bean-validation


【解决方案1】:

添加到您的持久性配置

<property name="javax.persistence.validation.group.pre-remov">MyDeleteGroup</property>

将此注释添加到您的日期字段

@Past(groups=MyDeleteGroup.class)

删除组MyDeleteGroup只是一个类似javax.validation.groups.Default的标记界面

【讨论】:

  • 完美运行!假设它不可能那么容易:)
  • 这是否意味着实体只有在实体存在约束违规时才能被删除?
  • @Sunnyday:这意味着只有在组'MyDeleteGroupe'的约束中没有约束VIOLATION时才能删除实体
【解决方案2】:

对于 Spring Boot 用户:

application.yml

spring:
  jpa:
    properties:
      javax:
        persistence:
          validation:
            group:
             pre-remove: com.your_package.MyDeleteGroup

application.properties

spring.jpa.properties.javax.persistence.validation.group.pre-remove=com.your_package.MyDeleteGroup

【讨论】:

    【解决方案3】:

    see this link 获取 BeanValidation 文档

    您可以为您的案例使用@Past 注释

    参见第 6.1 节

    <event type="pre-delete">
        <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
    </event>
    

    【讨论】:

    • @Past 是可以的,但是如何只在删除不保存时检查。
    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2018-09-09
    • 2013-10-23
    • 2020-02-20
    相关资源
    最近更新 更多