In essence, I specify:

/**Банковские реквизиты*/ @Column(name = "\"someBank\"", length = 300) private String someBank; 

In the database varchar(300) . Is it possible to get this length from the database / entity, for example, to limit the number of characters in the input field?

  • And the public file static stat SOME_BANK_LENGTH = 300 will not work? Or do you want to pick up the length of this request directly from the database? - Andrew Bystrov
  • @AndrewBystrov hm, then from a DB. - Artyom ... .- ....-.-

1 answer 1

This is the query that will return the column type:

 SELECT data_type FROM information_schema.columns WHERE table_name = 'YOUR_TABLE_NAME' AND column_name = 'YOUR_COLUMN_NAME'; 

Further from the line varchar(300) I think it will not be difficult to get the value 300

UPDATE. You can get the length from the annotation using reflection.

 int length = YourClassName.class.getField("someBank").getAnnotation(Column.class).length() 
  • It is more sensible to immediately get character_maximum_length or (what if you need it?) character_octet_length . See postgresql.org/docs/9.6/static/infoschema-columns.html - Akina
  • Yes thank you. But still you can not get the length of the annotations without the final fields? - Artyom ... .- ....-.-
  • @ Artem -.....-...-.- it is possible through reflection. See update response - Andrew Bystrov
  • @AndrewBystrov And the last question, how do I understand it is impossible to set the values ​​in the annotation in runtime so that the values ​​are taken directly from the database? - Artyom ... .- ....-.-
  • @ Artem -.....-...-.- no, it will not work that way, because the annotation does not have fields that you can put in runtime - Andrew Bystrov