简体   繁体   English

Java约束违规和HIbernate验证不会在更新时解决

[英]Java Constraint Violations and HIbernate Validations not woking on update

I have a domain object which looks like below 我有一个域对象,如下所示

@Entity
@Table(name = "zone")
public class Zone {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    long id;

    @Column(unique=true, nullable=false)
    String uuid;

    @NotNull
    @Size(min=1)
    @Column(unique=true, nullable=false)
    String name;

    @NotNull
    @Size(min=1)
    @Column(nullable=false)
    String admin;
}

There are two service calls which save and update the Zone objects 有两个服务调用可以保存和更新Zone对象

@Override
public Zone create(Zone zone) {

    if((zone.getUuid()==null)||(zone.getUuid().isEmpty())){
        String uuid = UUID.randomUUID().toString();
        zone.setUuid(uuid);
    }
    zoneRepository.save(zone);
    return zone;
}

@Override
public Zone update(Zone zone) {
    zoneRepository.save(zone);
    return zone;
}

Zone repository extends JPA Repository of spring-data framework 区域存储库扩展了spring-data框架的JPA Repository

public interface ZoneRepository extends JpaRepository<Zone, Long>

I am using unit test cases to test the two services 我正在使用单元测试用例来测试这两个服务

@Test(expected=ConstraintViolationException.class)
public void createZoneWithBlankName(){
    Zone zone = new Zone();
    zone.setName("");
    zone.setAdmin("Admin");
    zoneService.create(zone);
} 

This test case is throwing ConstraintViolationException as expected when name is blank. 当名称为空时,此测试用例会按预期抛出ConstraintViolationException。 But when I am updating a already existing zone with a blank name, no ConstraintViolationException is thrown and the test case below fails 但是当我使用空白名称更新已存在的区域时,不会抛出ConstraintViolationException并且下面的测试用例失败

@Test(expected=ConstraintViolationException.class)
public void updateZoneWithBlankName(){
    Zone zone = new Zone();
    zone.setName("Zone");
    zone.setAdmin("Admin");
    zoneService.create(zone);
    Assert.assertTrue(zone.getId() != 0);

    long id = zone.getId();
    String newZoneName = "";
    String newAdmin = "New Admin";

    Zone zoneToUpdate = new Zone();
    zoneToUpdate.setId(id);
    zoneToUpdate.setName(newZoneName);
    zoneToUpdate.setAdmin(newAdmin);

    zoneService.create(zoneToUpdate);

}

If I set the id of "zoneToUpdate" object to any value other than the existing id, then the constraint violation is thrown. 如果我将“zoneToUpdate”对象的id设置为除现有id之外的任何值,则抛出约束违规。 Which indicates that the exception is thrown only for creating new objects and not for updating the existing ones. 这表示仅为创建新对象而不是更新现有对象而抛出异常。

The reason you are not seeing the exceptions for updates is that the current unit of work is not getting flushed to the underlying database. 您没有看到更新例外的原因是当前的工作单元没有被刷新到底层数据库。

And that's to be expected: that's how the 1st-level cache works in Hibernate and JPA. 这是可以预料的:这就是第一级缓存在Hibernate和JPA中的工作方式。

For details, search for the "false positives" tip in the Testing chapter of the Spring Reference manual . 有关详细信息,请在Spring Reference手册Testing一章中搜索“误报”提示。

Regards, 问候,

Sam ( author of the Spring TestContext Framework ) Sam( Spring TestContext Framework的作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM