0

I've added hibernate validator to my project, and annotated my class with the relevant constraints. This is my pom.xml:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

and this is the annotated class:

public class EmployeeProject extends PanacheEntity {

    @NotNull
    @ManyToOne
    private Employee employee;

    @NotNull
    @ManyToOne
    private Project project;

    @Column
    @Min(value = 1)
    private int quantity;

}

When I try to persist a not valid bean - null employee/project and/or non-positive quantity - through Java code, a validation exception is raised, as expected.

However, when I try to persist a not valid tuple directly into the database, surprisingly no exceptions or DB errors are raised. Indeed the reason is that the employee_project auto-generated by Hibernate have no validation constraints - just the two foreign keys constraints. I know there is no simple way to translate @Min in SQL, but boy I was at least expecting a NOT NULL on employee_id and project_id!

Is this the regular behavior, or am I missing something?

Andrea
  • 675
  • 1
  • 13
  • 34

1 Answers1

1

This is explained in the @Min javadoc:

null elements are considered valid.

A null value is an undefined values, with @Min you are saying that, if the value is defined, it must be at least something.

The other constraints are missing because @ManyToOne default value is optional=true and the JPA annotation has precedence over the Bean Validation one when it comes to the creation of the DB.

It makes sense because somebody might want to validate columns on the database that are without constraints.

It will work if you use @ManyToOne(optional=false).

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • I see. This explain why I get no additional constraints for `quantity` field. But what about `employee_id` and `project_id`? They are annotated with `@NotNull` but there is no `NOT NULL` constraints on the DB for them. – Andrea Mar 28 '21 at 09:55
  • https://stackoverflow.com/questions/7439504/confusion-notnull-vs-columnnullable-false-with-jpa-and-hibernate – Davide D'Alto Mar 28 '21 at 09:58
  • Actually, it should work. Did you generate the db using Hibernate? With `create` or `create-drop` option? – Davide D'Alto Mar 28 '21 at 10:08
  • This will drop your database and recreate it: `quarkus.hibernate-orm.database.generation=drop-and-create` – Davide D'Alto Mar 28 '21 at 10:10
  • Can you push a test project somewhere? It might be a bug with Panache. You could try to not use panache and see if it works correctly – Davide D'Alto Mar 28 '21 at 10:15
  • The database was generated with the `drop-and-create` option, but the `employee-project` table was created with the `update` option. That's because in the meantime I already added some `employee`s and `project`s to the database, and I didn't want to lose them. However, I can do a backup of the data and create-drop the whole database if that's the issue here. – Andrea Mar 28 '21 at 10:27
  • Ah... I know what's happening, the `@ManyToOne` has the precedence over the `@NotNull`. Because the `@ManyToOne` default value is `optional=true`, hibernate has created the columns without the constraints – Davide D'Alto Mar 28 '21 at 10:39