Java JPA嵌入式和BeanValidation
我需要在数据库中将类Poo保存为null,因此表中已经存在的任何属性都将转换为null。有可能吗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
我唯一能做的就是从属性中删除@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;
[...]
}