Search code examples
cachingjpaeclipselinkglassfish-3entities

Char(3) field @Id is not cacheable in eclipselink jpa


For historic reasons i have some tables with char(3) primary key in our database. For example the country table.

When i find the entity with:

String id = "D  ";
Country c = em.find(Country.class, id);

i can see afterwards:

c.getId()  --> "D" and not "D  "

The entitiy is read again and again from the database. The caching does not work for some reason. I guess in the cache the id is existing as "D" and not as "D ".

20130423 09:15:14,495 FINEST   query                          Execute query     ReadObjectQuery(name="country" referenceClass=Country )
20130423 09:15:14,498 FINEST   connection                     reconnecting to external connection pool
20130423 09:15:14,498 FINE     sql                            SELECT countryID, countryNAME, countryTELCODE, countryTOPLEVELDOMAIN, countryINTTELPREFIX FROM geo.COUNTRY WHERE (countryID = ?)
bind => [D  ]
20130423 09:15:14,508 FINEST   query                          Execute query ReadObjectQuery(name="country" referenceClass=Country )
20130423 09:15:14,508 FINEST   connection                     reconnecting to external connection pool
20130423 09:15:14,508 FINE     sql                            SELECT countryID, countryNAME, countryTELCODE, countryTOPLEVELDOMAIN, countryINTTELPREFIX FROM geo.COUNTRY WHERE (countryID = ?)
bind => [D  ]

I tried to set the @Column(length=3) but it has no effect.

Does anybody has an idea why the cache does not work like it should.

Thanks Hasan


Solution

  • By default EclipseLink trims trailing spaces from CHAR fields. This can be disabled.

    See, http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_avoid_trimming_the_trailing_spaces_on_a_CHAR_field.3F