Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EclipseLink JPA to bind Strings larger than 255 characters

I inherited a project which uses EclipseLink JPA to persist objects into any SQL database. Currently it comes with a local Derby DB distribution. During some tests I found out that the program will throw the following exception:

012-08-03 10:21:11.357--UnitOfWork(32349505)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLDataException: A truncation error was encountered trying to shrink VARCHAR 'some necessarily really long text' to length 255. Error Code: 20000

Obviously VARCHAR isn't (usually) suitable for storing Strings larger than 255 characters yet I didn't find the code fragment where the objects variable is explicitely assigned to a VARCHAR field. I understand that JPA or EclipseLink automatically assigns this for you, so my question, where I couldn't find a simple answer yet, is:

How can I make sure that EclipseLink / JPA stores Strings larger than 255 characters?

Cheers

like image 617
The-Stig Avatar asked Aug 03 '12 09:08

The-Stig


3 Answers

You need to annotate the property so it gets persisted as Clob. Use @Lob; for Strings this will default to Clob. See here for the documentation. Of course you need to make sure that the database column type is correct (i.e. not VARCHAR) if you create the database manually.

like image 166
Hauke Ingmar Schmidt Avatar answered Sep 28 '22 17:09

Hauke Ingmar Schmidt


In many enviroments @Column of type String has limit larger than 255, for example to 4kB. You need use 'length' to change default 255

@Column(length=1024)

String sample;

like image 25
Jacek Cz Avatar answered Sep 28 '22 17:09

Jacek Cz


I show you data type that to bind String larger than 255 characters. See this;

Java Type - byte[], java.lang.Byte[], java.sql.Clob

Java DB, Derby, CloudScape - CLOB(64000)

Oracle - LONG

DB2 - CLOB(64000)

Sybase - TEXT

MSSQL - TEXT

MySQL - TEXT(64000)

like image 23
Sai Ye Yan Naing Aye Avatar answered Sep 28 '22 19:09

Sai Ye Yan Naing Aye