Search code examples
javasql-serverhibernatehbm2ddl

Creating table with longer text field by hibernate.hbm2ddl.auto


I am using hibernate.hbm2ddl.auto to create my tables.

Creating my column like this:

@Column(name = "foo", length = 8000)
private String foo;

This creates a table and the field : varchar(8000)

What I need, is that I can make save a longer string than 8000 in there. Like 20000 or more.

When changing the length higher than 8000, it won't create the table.

When changing the field manually, then it says:

The size (20000) given to the column 'foo' exceeds the maximum allowed for 
any data type (8000).

Solution

  • As recommended to use Clob for such large string column

    @Lob indicates that the property should be persisted in a Blob or a Clob depending on the property type: java.sql.Clob, Character[], char[] and java.lang.String will be persisted in a Clob. java.sql.Blob, Byte[], byte[] and serializable type will be persisted in a Blob.

    @Lob
    public String getFullText() {
        return fullText;
    }
    
    @Lob 
    public byte[] getFullCode() {
        return fullCode;
    }
    

    If the property type implements java.io.Serializable and is not a basic type, and if the property is not annotated with @Lob, then the Hibernate serializable type is used.

    Check example here

    http://tonyyan.wordpress.com/2008/10/28/clob-and-blob-saved-through-hibernate/