Home > front end >  JPA cache ingnore new parameters in NamedQuery
JPA cache ingnore new parameters in NamedQuery

Time:07-06

I'm not a JPA expert. Sometimes I must fix bugs in old code :(

Now I have a station-zip table like

CREATE TABLE `station_zip` (
  `ID` bigint(20),
  `CREATED_AT` datetime,
  `CREATED_AT_XML` datetime,
  `MODIFIED_AT` datetime DEFAULT NULL,
  `UPD_VERSION` bigint(20) DEFAULT NULL,
  `ZIP_CODE` int(11) DEFAULT NULL,
  `STATION_ID` int(11) DEFAULT NULL,
   primary key (id)
)

the entity definition in JPA code is:

@Cache(expiry = 300000, size = 10000) // 5 min

@Table(name = "station_zip")
@NamedQueries({
        @NamedQuery(
                name = StationZipBE.FIND_BY_ZIP_CODE,
                query = "select z from StationZipBE z where z.zipCode = :zipCode")
})
public class StationZipBE {
...
}

the access code is:

try {
            final Query namedQuery = getEntityManager().createNamedQuery(StationZipBE.FIND_BY_ZIP_CODE);
            namedQuery.setParameter("zipCode", zipCode);
            return (StationZipBE) namedQuery.getSingleResult();
        } 
catch (NoResultException e) {
...
}

The problem is:

I start the query for the first ZIP an get the right station ID.

I start the query for another ZIP (by unsing setParameter) but don't get the correct station ID. I got the ID from the first ZIP. Strange! If I set the cache expiry = 1 the query work correct?

It is a cache problem and how can I fix it?

CodePudding user response:

The problem was not a Bug in Code. My data in DB had the column ID. JPA use this ID for referencing rows. But here was the problem. All rows had the value ID=0! So no referencing is possible. After updating ID with unique numbers, my JPA problem was solved.

I think it's better use autoincrement for the ID column like:

  `ID` bigint(20) auto_increment,
  •  Tags:  
  • jpa
  • Related