Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Hibernate map the SQL data-type nvarchar(max)?

I have a column in my SQL-2005 database that used to be a varchar(max), but it has been changed to an nvarchar(max).

Now I need to update my hibernate mapping file to reflect the change, and this is what it used to be:

<element type="text" column="Value"/>

When I try to run the application, the following error appears:

org.hibernate.HibernateException: Wrong column type in [Table] for column Value. Found: ntext, expected: text

What should I put in the 'type' attribute to correctly map the column as an nvarchar(max)?

I've tried setting the type to ntext, but hibernate didn't know what that was. I tried setting the type to string, but it treated string as a text type.

like image 703
ampersandre Avatar asked Jul 08 '09 17:07

ampersandre


People also ask

How is nvarchar Max stored?

The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.

What is nvarchar Max?

The NVARCHAR(Max) data type stores variable-length character strings. NVARCHAR(Max) is used to store very large character data. NVARCHAR(Max) can hold as much as 2GB of Unicode character data. The word NVARCHAR stands for national varying character.

Can you index nvarchar Max?

Columns with data types like nvarchar(max), text, and ntext cannot be used in indexes.

What is the difference between nvarchar 50 and nvarchar Max?

nvarchar max is for columns up to 2GB. So essentially it takes up more resources. You are better off using the nvarchar(50) if you know you aren't going to need that much space. each character is about 2 bytes so with 2 GB thats 1 billion characters...


1 Answers

What worked for me is to put the actual column definition in a @Column annotation:

    @Column(name="requestXml", columnDefinition = "ntext")
private String request;
like image 109
Vaal Avatar answered Sep 20 '22 16:09

Vaal