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

JavaSql ServerHibernateJpaDdl

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

Hi the below code fixed the same issue

@Column(columnDefinition="TEXT")

@Lob

final String someString;

Solution 4 - Java

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

Solution 5 - Java

You Can Use this Code -

Model Code:

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

SQL Output is like-

> gender varchar(7)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTobbView Question on Stackoverflow
Solution 1 - JavaTobbView Answer on Stackoverflow
Solution 2 - JavaDataNucleusView Answer on Stackoverflow
Solution 3 - JavaKagiso ShibamboView Answer on Stackoverflow
Solution 4 - JavaAnudeep SharmaView Answer on Stackoverflow
Solution 5 - Javacm_mehdiView Answer on Stackoverflow