@Entity
public class Foo {
[...]
@Valid
@Embedded
private Poo poo;
}
@Embeddable
public class Poo {
@NotNull
private Double value;
@NotNull
private Double value2;
[...]
}
I need to save the class Poo as null in the database and consequently any attribute that already exists in the table is converted to null. It's possible?
The only way I could do it was to remove @NotNull from the attributes and set each attribute itself to null.
Just had the same problem and I solved it by adding an Annotation like @NotNull just underneath the annotation @Valid
@Entity
public class Foo {
[...]
@Valid
@Embedded
@NotNull
private Poo poo;
}
@Embeddable
public class Poo {
@NotNull
private Double value;
@NotNull
private Double value2;
[...]
}
By default for me it works exactly as you said. I just tested it with MariaDB and:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
And here's my test code:
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);
But if you still have a problem, please, provide your test scenario and check out this post: https://stackoverflow.com/a/40979958/7974082 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.