@NotNull annotation does not translate to not null constraint in schema generation

问题: According to hibernate validator reference documentation - Section 11.1.1. Database schema-level validation (https://docs.jboss.org/hibernate/stable/validator/reference/en-...


According to hibernate validator reference documentation - Section 11.1.1. Database schema-level validation (https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-checkconstraints-db),

Out of the box, Hibernate ORM will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity is annotated @NotNull, its columns will be declared as not null in the DDL schema generated by Hibernate ORM.

But when I annotate my fields with @NotNull, I'm not getting the schema level constraints automatically.

I tried setting Environment.HBM2DDL_AUTO to update and create but neither one works in generating the database constraints.

Even though hibernate by default has the hibernate.validator.apply_to_ddl set to true, I even tried setting that manually -

settings.put("hibernate.validator.apply_to_ddl", true);

But none of these options seems to work for me.

My domain class

package com.praveen.domain;

import javax.persistence.Entity;
import javax.validation.constraints.NotNull;

import lombok.Data;

public class Stakeholder extends MyPersistent {

    private String clientCode;

    private String gender;

MyPersistent class

package com.praveen.domain;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import lombok.Getter;
import lombok.Setter;

public class MyPersistent implements Serializable {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

The code I'm using to get the session factory is

    private static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();

                Map<String, Object> settings = new HashMap<>();
                settings.put(Environment.DRIVER, Driver.class.getName());
                settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_test?"
                        + "zeroDateTimeBehavior=CONVERT_TO_NULL&"
                        + "createDatabaseIfNotExist=true&"
                        + "useSSL=true&"
                        + "verifyServerCertificate=false&"
                        + "autoReconnect=true");
                settings.put(Environment.USER, "root");
                settings.put(Environment.PASS, "");
                settings.put(Environment.DIALECT, MySQL55Dialect.class);
                settings.put(Environment.HBM2DDL_AUTO, "update");
                settings.put(Environment.SHOW_SQL, false);

                // HikariCP settings
                settings.put("hibernate.hikari.connectionTimeout", "20000");
                settings.put("hibernate.hikari.minimumIdle", "5");
                settings.put("hibernate.hikari.maximumPoolSize", "20");
                settings.put("hibernate.hikari.idleTimeout", "300000");

                registry = registryBuilder.build();
                MetadataSources sources = new MetadataSources(registry);

                Metadata metadata = sources.getMetadataBuilder().build();
                sessionFactory = metadata.getSessionFactoryBuilder().build();
            } catch (Exception e) {
        return sessionFactory;


You should use @Column(nullable = false)

@NotNull is for Bean validation, not schema constraints, and if you are using an older version of hibernate @NotNull will not be translated to column constraint

  • 发表于 2018-07-11 16:54
  • 阅读 ( 246 )
  • 分类:sof


请先 登录 后评论


作家榜 »

  1. 小编 文章