Here's another idea... When did these entities get persisted to the
database? And, are they still part of the persistence context that is being
used for the query?
Thanks to everyone for their responses. Removing the quotes from the int
values in the SQL statement changed things a bit, but I'm still not there.
The entity Platform was created directly with the MySQL Query Browser in a
5.1.37 database using the DDL listed;
CREATE TABLE ids2.Platform (
id INT NOT NULL AUTO_INCREMENT
, PlatformName VARCHAR(32)
, PRIMARY KEY (id)
) ENGINE = InnoDB;
ALTER TABLE ids2.Platform MODIFY COLUMN PlatformName VARCHAR(32)
COMMENT 'Platform Description';
The table was then filled manually using INSERT INTO sql statements so the
table and it's contents were created externally to any JPA implementations.
My test cases now read
@Test
public void listPlatforms() {
String jpql = "select c from Platform c";
List<Platform> entities = (List) em.createQuery(jpql).getResultList();
assertEquals("List size:" , 9 , entities.size());
assertEquals("Id", 8, entities.get(7).getId());
}
@Test
public void listPlatformsNative() {
String jpql = "select * from Platform";
List<Platform> entities = (List)
em.createNativeQuery(jpql,Platform.class).getResultList();
assertEquals("List size:" , 9 , entities.size());
assertEquals("Id", 8, entities.get(7).getId());
}
@Test
public void getPlatformById() {
Platform platform = (Platform) em.find(Platform.class, 8);
assertNotNull(platform);
assertEquals("PlatformName", "record8", platform.getPlatformName());
assertEquals("Id",8,platform.getId());
}
@Test
public void getPlatformByQueryById() {
String jpql = "Select c from Platform c where c.id=8";
Platform platform = (Platform) em.createQuery(jpql).getSingleResult();
assertNotNull(platform);
assertEquals("PlatformName", "record8", platform.getPlatformName());
assertEquals("Id",8,platform.getId());
}
@Test
public void getPlatformByNativeQueryById() {
String jpql = "Select * from Platform where id=8";
Platform platform = (Platform)
em.createNativeQuery(jpql,Platform.class).getSingleResult();
assertNotNull(platform);
assertEquals("PlatformName", "record8", platform.getPlatformName());
assertEquals("Id",8,platform.getId());
}
All the tests are failing on the id assertion, returning 0 in all cases.
I've set the openjpa.Log property to SQL=TRACE and the results from the
tests are below. They don't seem terribly informative, is there a
different/alternative setting I should be using?
78 ids2 INFO [main] openjpa.Runtime - Starting OpenJPA 1.2.1
250 ids2 INFO [main] openjpa.jdbc.JDBC - Using dictionary class
"org.apache.openjpa.jdbc.sql.MySQLDictionary".
11422 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 18414151>
executing prepstmnt 32512553 SELECT t0.id, t0.PlatformName FROM Platform t0
11422 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 18414151> [0
ms] spent
16547 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 29594051>
executing prepstmnt 27660658 select * from Platform
16563 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 29594051>
[16 ms] spent
21594 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 22538826>
executing prepstmnt 17427094 SELECT t0.PlatformName FROM Platform t0 WHERE
t0.id = ? [params=(int) 8]
21594 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 22538826> [0
ms] spent
26641 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 27140443>
executing prepstmnt 1668655 SELECT t0.id, t0.PlatformName FROM Platform t0
WHERE (t0.id = ?) [params=(long) 8]
26641 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 27140443> [0
ms] spent
31672 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 12273995>
executing prepstmnt 28318458 Select * from Platform where id=8
31672 ids2 TRACE [main] openjpa.jdbc.SQL - <t 12590745, conn 12273995> [0
ms] spent
Regards
--
View this message in context: http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3608883.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
|