Java – Using EclipseLink JPA to bind Strings larger than 255 characters

eclipselinkjavajpasql

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

Best Answer

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.

Related Topic