Skip to content

Bean Validation seems weird with lazy fetch attribute. #3888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
onacit opened this issue May 16, 2025 · 1 comment
Closed

Bean Validation seems weird with lazy fetch attribute. #3888

onacit opened this issue May 16, 2025 · 1 comment
Labels
for: external-project For an external project and not something we can fix

Comments

@onacit
Copy link

onacit commented May 16, 2025

Apologies. This issue may be just a question.

I have an entity with a lazy-fetch -to-one attribute.

@MappedSuperclass
abstract class MappedParent {

    @NotNull
    private String a;
}

@MappedSuperclass
abstract class MappedChild<PARENT extends MappedParent> {

    @NotNull
    @Basic(optional = false)
    @Column(name = "parent_id", nullable = false, insertable = true, updatable = false)
    private Long parentId;

    @Valid // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @NotNull
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", nullable = false, insertable = true, updatable = false)
    private <PARENT> parent;
}

@Entity
class Parent extends MappedParent {
}

@Entity
class Child extends MappedChild<Parent> {
}

When I, in my test code, select one and assure that the entity is valid, the framework (or library) tries to validate fields on.

@Transactional
@SpringBootTest
class SomeTest { 

    @Autowired
    private EntityManager entityManager;

    void () {
        final var found = entityManager.find(...)
        BeanValidationTestUtils.requireValid(selected); // <<<<<<<<<<<<<<<<<<<<<<<
    }

The BeanValidationTestUtils.requireValid(selected); looks like this. Nothing's special.

    public static <T> Set<ConstraintViolation<T>> validate(Validator validator, final T object,
                                                           final Class<?>... groups) {
        Objects.requireNonNull(validator, "validator is null");
        Objects.requireNonNull(object, "object is null");
        Objects.requireNonNull(groups, "groups is null");
        return validator.validate(object, groups);
    }

    public static <T> T requireValid(final Validator validator, final T object, final Class<?>... groups) {
        final var violations = validate(validator, object, groups);
        if (!violations.isEmpty()) {
            throw new ConstraintViolationException(violations);
        }
        return object;
    }

The moment just before validating the entity looks like this.

selected = (Child ...)
    parent_id = xx
    parent = (Parent@HibernateProxy@nnnn; a=<real, not-null>, b = <real; not-null>)
        $$_hibernate_interceptor
        a = null
        b = null

And the validation fails tries to validate parent.a.

parent.a: must not be null,

When I comment-out the @Valid annotation, leaving the @NotNull annotation, it passes.

    //@Valid // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @NotNull
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", nullable = false, insertable = true, updatable = false)
    private <PARENT> parent;
  • I'm not sure this issue arose from the Hibernate
  • and/or Hibernate-Validator
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 16, 2025
@schauder
Copy link
Contributor

This has really nothing to do with Spring Data, but seems to be about Hibernate Validator.

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2025
@schauder schauder added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants