JPA - How to set string column to varchar(max) in DDL

后端 未结 4 1806
有刺的猬
有刺的猬 2020-12-09 01:14

With JPA, DDL-generation for the attribute:

@Column
final String someString;

will be someString varchar(255) null



        
相关标签:
4条回答
  • 2020-12-09 01:26

    Since length is defined in the JPA spec and javadocs as int type and max is not an int then it's safe to assume that you're consigned to the columnDefinition datastore-dependent route. But then varchar(max) is datastore-dependent anyway.

    0 讨论(0)
  • 2020-12-09 01:33

    You Can Use this Code -

    Model Code:

    @NotNull  
    @Length(max = 7)  
    @Column(name = "Gender")  
    private String gender;
    

    SQL Output is like-

    > gender varchar(7)
    
    0 讨论(0)
  • 2020-12-09 01:39

    Some months have passed, new knowledge acquired, so I'll answer my own question:

    @Lob
    @Column
    final String someString;
    

    yields the most correct result. With the version of hbm2ddl I'm using, this will be transformed to the type text with SqlServerDialect. Since varchar(max) is the replacement for text in newer versions of SQL Server, hopefully, newer versions of hbm2ddl will yield varchar(max) instead of text for this type of mapping (I'm stuck at a quite dated version of Hibernate at the moment..)

    0 讨论(0)
  • 2020-12-09 01:42

    Use @Size(max = 1337). It does generate varchar(1337)

    0 讨论(0)
提交回复
热议问题