Why does Hibernate Tools hbm2ddl generation not take into account Bean Validation annotations?

孤街浪徒 提交于 2019-12-01 20:11:40

You need to distinguish validation api and java persistence api (jpa) (and vendor specific persistence api). Hibernate take into account JPA configuration (and hibernate persistence api) and when you do not provide such configuration then Convention Over Configuration principle is involved to this process. It is why you get varchar(255) for

@Size(max = 50)
private String shortTitle;

it equals to (i omitted other default values)

@Size(max = 50)
@Column(length = 255, nullable = true)
private String shortTitle;

Validation api is involved for validation purposes. To check if the fields properly filled. There can be exist the different validation rules for the same field.


Updated

I mean this http://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintdefinition-groups.

For one group you validate one constraint, for other group you validate other constraint.

For example

@NotNull(groups = DefaultGroup.class)
@Null(groups = SecondGroup.class)
private String shortTitle;

and then

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Title>> constraintViolations = validator.validate(title, DefaultGroup.class);
    Set<ConstraintViolation<Title>> secondConstraintViolations = validator.validate(title, SecondGroup.class);
maimoona

Try removing @Size(max=50), use @Column(length = 50) only. Also add @Column(length = 50) to variable shortTitle.

@NotEmpty @Column(length = 50)
private String titlename;

/** User visible short version of the title. */
@Column(length = 50)
private String shortTitle;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!