Reputation: 79
@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.
Upvotes: 3
Views: 786
Reputation: 1212
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;
[...]
}
Upvotes: 4
Reputation: 6630
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 .
Upvotes: 1