@Column, length and string truncation

View: New views
4 Messages — Rating Filter:   Alert me  

@Column, length and string truncation

by Joe Mc. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have annotated a field for example with the following annotation:
@Column(name="lastMessage", length=255)
String message;

Further I insert in this field strings longer as 255 chars.
The SQL Server throws an exception, cause the string is to long.
Why does not Eclipselink truncate the string value to the specified length (here: 255) before the SQL Insert?

Does an other (configurable) ways exist?

Thanks

McJoe

Basic data:
- Eclipselink 1.1.1
- SQL Server 2005
- Java 5/6

Re: @Column, length and string truncation

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would think you would want an error if your data was being truncated.

JPA does not perform any validation of column sizes, there is a validation Spec (javax.validation I believe) that has some support for validation, but I think it would give you the error in Java, not truncate the data.

You may wish to truncate the data in your set methods, or UI, or possibly through a JPA or EclipseLink event.


Joe Mc. wrote:
Hi,

I have annotated a field for example with the following annotation:
@Column(name="lastMessage", length=255)
String message;

Further I insert in this field strings longer as 255 chars.
The SQL Server throws an exception, cause the string is to long.
Why does not Eclipselink truncate the string value to the specified length (here: 255) before the SQL Insert?

Does an other (configurable) ways exist?

Thanks

McJoe

Basic data:
- Eclipselink 1.1.1
- SQL Server 2005
- Java 5/6

Re: @Column, length and string truncation

by Joe Mc. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hm ok,
than is the "length" declaration only relevant for the DDL SQL statements?

Regards

McJoe




I would think you would want an error if your data was being truncated.

JPA does not perform any validation of column sizes, there is a validation Spec (javax.validation I believe) that has some support for validation, but I think it would give you the error in Java, not truncate the data.

You may wish to truncate the data in your set methods, or UI, or possibly through a JPA or EclipseLink event.


Joe Mc. wrote:
Hi,

I have annotated a field for example with the following annotation:
@Column(name="lastMessage", length=255)
String message;

Further I insert in this field strings longer as 255 chars.
The SQL Server throws an exception, cause the string is to long.
Why does not Eclipselink truncate the string value to the specified length (here: 255) before the SQL Insert?

Does an other (configurable) ways exist?

Thanks

McJoe

Basic data:
- Eclipselink 1.1.1
- SQL Server 2005
- Java 5/6


Re: @Column, length and string truncation

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes.  length and most of the @Column attributes are only relevant to DDL generation.


Hm ok,
than is the "length" declaration only relevant for the DDL SQL statements?

Regards

McJoe



James Sutherland wrote:
I would think you would want an error if your data was being truncated.

JPA does not perform any validation of column sizes, there is a validation Spec (javax.validation I believe) that has some support for validation, but I think it would give you the error in Java, not truncate the data.

You may wish to truncate the data in your set methods, or UI, or possibly through a JPA or EclipseLink event.


Joe Mc. wrote:
Hi,

I have annotated a field for example with the following annotation:
@Column(name="lastMessage", length=255)
String message;

Further I insert in this field strings longer as 255 chars.
The SQL Server throws an exception, cause the string is to long.
Why does not Eclipselink truncate the string value to the specified length (here: 255) before the SQL Insert?

Does an other (configurable) ways exist?

Thanks

McJoe

Basic data:
- Eclipselink 1.1.1
- SQL Server 2005
- Java 5/6