问题
I have got the following Parent, Child and Subchild entities:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false, precision = 20)
private Long id;
@Valid
@BatchSize(size = 5)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent")
private List<Child> childs = new ArrayList<>();
...
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false, precision = 20)
private Long id;
@NotNull
@ManyToOne(optional = false)
@JoinColumn(name = "parent_id", nullable = false)
private Parent parent;
@Valid
@BatchSize(size = 5)
@Size(min = 1)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "child")
private List<Subchild> subchilds = new ArrayList<>();
....
}
@Entity
public class Subchild {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false, precision = 20)
private Long id;
@NotNull
@ManyToOne(optional = false)
@JoinColumn(name = "child_id", nullable = false)
private Child child;
....
}
Notice, that there is a @Size(min=1)
validation on the subchilds
association.
Parent, child and subchild are linked by a OneToMany association and the child and subchild are always updated (and persisted) together with the parent:
@Service
public class ParentService {
public Parent update(@NotNull @Valid Parent parent) {
return parentRepository.saveAndFlush(parent);
}
....
}
Until recently I used an Oracle database and the JPA sequence strategy to generate the primary keys. This worked fine so far:
- I could create parent entities together with some childs and subchilds
- I could also add some additonal childs with subchilds entites after the parent has already been saved.
However, now I'm using a Postgres database and the sequence strategy has been changed to Identity. The above 2nd use case does not work anymore. As soon as a parent is once saved, I can't add any childs afterwards. A bean validation exception occurs that says my subchilds are empty. However the collection is initialized and not empty in my ParentService, it is only empty in the validator. If I remove the @Size(min=1)
validator completely, everything is saved correctly. It seems to me that the subchild association is not correctly "passed on" to the bean validation.
This is an issue with GenerationType.Identity. If I am using GenerationType.Sequence everything works fine.
Any ideas?
Reproducable Testcase:
@RunWith(SpringRunner.class)
@DataJpaTest
public class ParentRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private ParentRepository parentRepo;
@Test
public void test() {
Parent parent = new Parent();
Parent savedParent = entityManager.persistAndFlush(parent);
Child child = new Child();
Subchild subchild = new Subchild();
subchild.setChild(child);
child.getSubchilds().add(subchild);
child.setParent(savedParent);
savedParent.getChilds().add(child);
parentRepo.saveAndFlush(savedParent); // it throws a validation error here
}
来源:https://stackoverflow.com/questions/63131834/bean-validation-on-associations-not-working-when-using-generation-strategy-ident