0

I try to understand which annotation for hibernate field have highest precedence. I use @Column, @Length and @Size for the same field and want to know which value will be taken to set column length in a database.

    @Column(name = "var", length = 5)
    @Length(max = 167)
    @Size(max = 64)
    private String var;

Using above code, column length in database is set to 167.

'Hibernate: create table test (id int4 not null, var varchar(167), primary key (id))'

    @Column(name = "var", length = 5)
    @Length(max = 168)
    @Size(max = 64)
    private String var;

But if I use this code I got column length equals to 64.

'Hibernate: create table test (id int4 not null, var varchar(64), primary key (id))'

Could you help me understand this? Do you know any reference which completely describe meaning of @Column, @Length and @Size annotation?

konrado
  • 185
  • 1
  • 1
  • 8
  • 1
    `@Size` and `@Length` are annotations used for validation and normally don't influence the schema that is being created. Hibernate however can choose to also take that into account whilst generating a schema. (In reality apart from a unit test you should never let hibernate manage your schema!). You can disable this with a property (see https://stackoverflow.com/questions/36260033/restrict-hibernate-to-not-recognize-validation-annotations-on-ddl-generation). Hibernate mainly uses that to also add not-null constraints for `@NotNull`and `@NotEmpty` but takes everything else into account as well. – M. Deinum Jun 05 '19 at 09:10
  • It solved one problem - now value ```@Column``` length is always taken as column length in database. But I am still curious about explanation of my example. Why hibernate once choose ```@Length``` value and in other case ```@Size```? – konrado Jun 05 '19 at 09:33

0 Answers0