Behavior in detailview

Hi everyone, I have a detailView of class A with some fields and a datagrid to a class B (o2m relationship) , the B class has a boolean field “principal”. I must assure that there is only one B with principal to true. So i write a method to do that in the “beforeSave” of B detailview, but for some reason the other records remains doesn’t change their principal value. Any ideas why? in the before save code it’s like this
if record.getPrincipal()){ record.getA().getB().stream() .filter(Objects::nonNull) .filter(p -> !record.getCode().equals(p.getCode())) .forEach(p -> p.setPrincipal(false)); }
Thanks