61

With JPA, DDL-generation for the attribute:

@Column
final String someString;

will be someString varchar(255) null

@Column(length = 1337)
final String someString;

will yield someString varchar(1337) null.

But how can I get it to produce someString varchar(max) null?

Is it possible using the length-attribute, or do I need to use the columnDefinition-attribute?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Tobb
  • 11,850
  • 6
  • 52
  • 77

5 Answers5

118

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..)

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Tobb
  • 11,850
  • 6
  • 52
  • 77
  • Providing a custom SQLServerDialect that maps a text lob to varchar(max) might also do the trick, haven't tried though. – Tobb Jul 31 '18 at 17:03
0

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • 3
    Other places in the project large texts were annotated with `@Lob`, which results in a `text` SQL-datatype. Will do the same.. – Tobb May 07 '13 at 11:13
-2

Hi the below code fixed the same issue

@Column(columnDefinition="TEXT")

@Lob

final String someString;
  • 1
    This does not provide any improvement to existing answers, it has both @Lob and columnDefinition that does the same, and the use of columnDefinition unnecessarily breaks portability. – Tobb Mar 26 '21 at 10:50
-5

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

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • @aycanadal, just in case you did not find the answer, check: https://stackoverflow.com/questions/34588354/difference-between-size-length-and-columnlength-value-when-using-jpa-and-h/34589474#34589474 – eprats Jan 30 '19 at 15:33
  • I did not downvote, but I know why. https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-2017 varchar(max) .. "max" is not a placeholder for a number (where you have 1337) .. varchar(max) (no substitutions) is a datatype in sql server. – granadaCoder Mar 06 '19 at 21:06
-5

You Can Use this Code -

Model Code:

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

SQL Output is like-

> gender varchar(7)
cm_mehdi
  • 73
  • 1
  • 1
  • 6