Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA嵌入式和BeanValidation_Java_Jpa_Eclipselink_Bean Validation - Fatal编程技术网

Java JPA嵌入式和BeanValidation

Java JPA嵌入式和BeanValidation,java,jpa,eclipselink,bean-validation,Java,Jpa,Eclipselink,Bean Validation,我需要在数据库中将类Poo保存为null,因此表中已经存在的任何属性都将转换为null。有可能吗 我唯一能做的就是从属性中删除@NotNull,并将每个属性本身设置为null。默认情况下,它的工作原理与您所说的完全相同。我刚刚用MariaDB测试了它,并且: @Entity public class Foo { [...] @Valid @Embedded private Poo poo; } @Embeddable public class Poo

我需要在数据库中将类Poo保存为null,因此表中已经存在的任何属性都将转换为null。有可能吗


我唯一能做的就是从属性中删除@NotNull,并将每个属性本身设置为null。

默认情况下,它的工作原理与您所说的完全相同。我刚刚用MariaDB测试了它,并且:

@Entity    
public class Foo {

    [...]

    @Valid
    @Embedded
    private Poo poo;
}

@Embeddable
public class Poo {

    @NotNull
    private Double value;

    @NotNull
    private Double value2;

    [...]
}

org.eclipse.persistence

.

也有同样的问题,我通过在注释@Valid下面添加一个类似@NotNull的注释来解决它

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    Foo foo = new Foo(1, new Poo(1, 2));
    validate(validator, foo);
    entityManager.merge(foo);

    List<Foo> fooList = entityManager
        .createNamedQuery(Foo.FIND_ALL, Foo.class)
        .getResultList();
    assertTrue(fooList.get(0).getPoo().getValue1() == 1);

    foo.setPoo(null);
    validate(validator, foo);
    entityManager.merge(foo);

    fooList = entityManager
            .createNamedQuery(Foo.FIND_ALL, Foo.class)
            .getResultList();

    assertTrue(fooList.get(0).getPoo() == null);
有关如何指定EclipseLink应如何处理空值的信息,请参见。
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    Foo foo = new Foo(1, new Poo(1, 2));
    validate(validator, foo);
    entityManager.merge(foo);

    List<Foo> fooList = entityManager
        .createNamedQuery(Foo.FIND_ALL, Foo.class)
        .getResultList();
    assertTrue(fooList.get(0).getPoo().getValue1() == 1);

    foo.setPoo(null);
    validate(validator, foo);
    entityManager.merge(foo);

    fooList = entityManager
            .createNamedQuery(Foo.FIND_ALL, Foo.class)
            .getResultList();

    assertTrue(fooList.get(0).getPoo() == null);
    @Entity    
public class Foo {

    [...]

    @Valid
    @Embedded
    @NotNull
    private Poo poo;
}

@Embeddable
public class Poo {

    @NotNull
    private Double value;

    @NotNull
    private Double value2;

    [...]
}