What does the length attribute do when set on the @Column JPA annontation?

后端 未结 3 1166
臣服心动
臣服心动 2020-12-04 15:32

What exactly does setting the length on a column do in JPA?

@Column(name = \"middle_name\", nullable = false, length = 32)
public String getMiddleName() {
           


        
3条回答
  •  無奈伤痛
    2020-12-04 15:38

    Hibernate 4.3.11 (and other Versions) should pay attention to Validation Annotations. - so you maybe have to upgrade

    This are cites from Hibernate 4.3.11 manual

    Chapter 22.Additional modules

    Hibernate Core also offers integration with some external modules/projects. This includes Hibernate Validator the reference implementation of Bean Validation (JSR 303) and Hibernate Search.

    Chapter 22.1 Bean Validation

    ... The integration between Hibernate and Bean Validation works at two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the Hibernate metamodel and incorporate them into the generated database schema. ...

    Chapter 22.1.4 Database schema

    Hibernate uses Bean Validation constraints to generate an accurate database schema:

    @NotNull leads to a not null column (unless it conflicts with components or table inheritance)
    
    @Size.max leads to a varchar(max) definition for Strings
    
    @Min, @Max lead to column checks (like value <= max)
    
    @Digits leads to the definition of precision and scale (ever wondered which is which? It's easy now with @Digits :) )
    

    Note: @Lengh works too, like @Size


    When you use Hibernate Validator 5.1 - then you also need an el-Implementation. For example

    
        org.glassfish.web
        el-impl
        2.2
    
    

    If you do not have this, then Hibernate ORM will not been able to start Hibernate Validation, ad therefore it would not take (all) JSR-303 for example @Length, @Size in account!

提交回复
热议问题