@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;

@Entity
@Data
public class Stakeholder extends MyPersistent {

    @NotNull
    private String clientCode;

    @NotNull
    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;

@MappedSuperclass
@Getter
@Setter
public class MyPersistent implements Serializable {

    @Id
    @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");

                registryBuilder.applySettings(settings);
                registry = registryBuilder.build();
                MetadataSources sources = new MetadataSources(registry);
                sources.addAnnotatedClass(Stakeholder.class);

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

回答1:

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. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除