【问题标题】:JSR303 validation doesn't validate if @Id is not @GeneratedValue with Spring Data Jpa and HibernateJSR303 验证不验证 @Id 是否不是 @GeneratedValue 与 Spring Data Jpa 和 Hibernate
【发布时间】:2013-08-19 04:22:45
【问题描述】:

在一个简单的项目中,我喜欢测试 @NotNull 验证(以及其他一些自定义验证)。

因此,我编写了一些执行此操作的单元测试:@Test(expect=ValidationException.class

一个最小的 mavinized 示例来重现我在 github 上上传的问题:

我认为如果@Id 是生成值,它会很好地工作。但是如果@Id是系统给出的,则忽略验证。

这些类将显示重现问题的最小设置:

两个实体(一个有生成值,一个没有生成值:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class GeneratedId {
    @Id
    @GeneratedValue
    private Long    id;

    @NotNull
    private String  content;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class GivenId {
    @Id
    private Long    id;

    @NotNull
    private String  content;
}

单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:/applicationContext.xml")
@Transactional
@ActiveProfiles("embedded")
public class MyEntityTest
{
    @Autowired GeneratedIdService   generatedIdService;

    @Autowired GivenIdService       givenIdService;

    // This test will pass
    @Test(expected = ValidationException.class)
    public void shouldNotAllowNullValues1()
    {
        this.generatedIdService.save(new GeneratedId());
    }

    // This test will fail
    @Test(expected = ValidationException.class)
    public void shouldNotAllowNullValues2()
    {
        this.givenIdService.save(new GivenId(1L, null));
    }
}

这是样板服务和存储库

public interface GeneratedIdRepository extends JpaRepository<GeneratedId, Long> {
}

public interface GivenIdRepository extends JpaRepository<GivenId, Long> {
}

@Service
public class GeneratedIdService {
    @Autowired GeneratedIdRepository    repository;

    public GeneratedId save(final GeneratedId entity) {
        return this.repository.save(entity);
    }
}

@Service
public class GivenIdService {
    @Autowired GivenIdRepository    repository;

    public GivenId save(final GivenId entity) {
        return this.repository.save(entity);
    }
}

目前我正在使用 Spring 3.1.4、Spring-Data 1.3.4、Hibernate 4.1.10 和 Hibernate-Validator 4.2.0。

有什么建议的方式跳过验证?

编辑 1:

我在两个实体上都尝试了没有lombok,仍然出现错误。

【问题讨论】:

    标签: java jpa spring-data spring-data-jpa lombok


    【解决方案1】:

    强制刷新验证有效:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath*:/applicationContext.xml")
    @Transactional
    @ActiveProfiles("embedded")
    public class MyEntityTest
    {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        @Autowired
        GeneratedIdService  generatedIdService;
    
        @Autowired
        GivenIdService      givenIdService;
    
        // This test will pass
        @Test(expected = ValidationException.class)
        public void shouldNotAllowNullValues1()
        {
            this.generatedIdService.save(new GeneratedId());
        }
    
        // This test will fail
        @Test(expected = ValidationException.class)
        public void shouldNotAllowNullValues2()
        {
            this.givenIdService.save(new GivenId(1L, null));
            entityManager.flush();
        }
    }
    

    另请参阅:this question

    【讨论】:

    • 感谢您的建议。只有当 ID 不是自动生成时,我们才需要刷新,这感觉很奇怪。如果这是要走的路,那似乎是一种解决方法或框架中的错误。我会 w8 获得其他一些答案。否则我会接受你的。谢谢
    【解决方案2】:

    如果您想强制持久性提供程序刷新EntityManager在事务提交或回滚之前,您需要手动刷新它或在JpaRepository 上使用saveAndFlush(…)。

    原因是在自动生成 ID 的情况下,持久性提供程序 必须刷新才能将 ID 绑定到 Java 对象。在手动分配 ID 的情况下,根本不需要在比事务结束时更早的时间点刷新,因此持久性提供程序避免了数据库交互。

    除了这些技术细节之外,我认为依赖持久性提供程序进行这种验证无论如何在架构上都是有问题的。如果提供者检测到违规行为,您实际上已经通过各种业务逻辑管道传递了一个无效对象。为了确保您不必进行防御性编码(每个地方都检查 null),您可以通过检查传递给构造函数或 setter 的值来简单地强制属性不可为空。这样,您基本上知道,无论何时获得对象的实例,该值永远不会是null,无论是否调用了某个第三个框架,或者某些开发人员意外忘记调用它。

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 2014-01-21
      • 2018-08-05
      • 2011-08-09
      • 2019-11-08
      • 2014-07-27
      • 1970-01-01
      • 2017-12-28
      相关资源
      最近更新 更多