The javax.persistence.Column
's length
attribute is used to define the column length of String
fields (it is ignored for other types) and is only used when the persistence framework will generate the database schema (several CREATE TABLE
s) from the entities, such as this option (for Hibernate on hibernate.cfg.xml
):
<property name="hbm2ddl.auto">create</property>
In your example, the column serialNo
would be created as a VARCHAR(12)
.
For all other purposes (inserting or retrieving data), it is ignored.
Also, it is useful if you want to "document" metadata information of your database in your classes. This way, yourself can validate the to-be-saved value before trying to insert and avoid any "value too long" or "data truncation"-like exceptions.