The-Stig
The-Stig

Reputation: 107

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

Upvotes: 5

Views: 9380

Answers (3)

Jacek Cz
Jacek Cz

Reputation: 1906

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;

Upvotes: 2

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

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)

Upvotes: 1

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

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.

Upvotes: 10

Related Questions