简体   繁体   中英

What happens if the length attribute is not specified on the @Column annontation for a String

What happen if I not specified the length attribute for following annotation? Note that region is a String.

@Column(name = "REGION", nullable = false)
public String getRegion() {
        return region;
    }

If you do not specify the length attribute then hibernate takes the default length which is 255 if the attribute type is String .

Check this @Column.length

If you don't use the length attribute nothing will happen default value is taken, exception will only be thrown when defined size in database side is exceeded ie varchar2(32) and entered value is more than that.

But yes to restrict the size you can use @Size(max=32) to have validation from code side.

The length attribute of the @Column annotation is only used during schema generation, viz:

@Column(name = "REGION", nullable = false, length=50)
public String getRegion() {
        return region;
    }

will create a column definition with length 50 if you are using your provider's schema generation functionality. If not, then it is of no relevance at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM