diff --git a/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java b/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java index 5f27b2294..7cd81e3ce 100644 --- a/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java +++ b/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java @@ -49,7 +49,11 @@ private PathDescriptor computePathDescriptor(String agPath) { if (last instanceof AgIdPart) { AgIdPart id = (AgIdPart) last; - return new PathDescriptor(id.getType(), PathOps.parsePath(id.getName()), true); + if (entity.getIdParts().contains(id)) { + return new PathDescriptor(id.getType(), PathOps.parsePath(id.getName()), true); + } else { + return new PathDescriptor(id.getType(), PathOps.parsePath(agPath), true); + } } AgRelationship relationship = (AgRelationship) last; diff --git a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java index f6bf34f70..f8e22b1ac 100644 --- a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java +++ b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java @@ -2,6 +2,7 @@ import io.agrest.AgException; import io.agrest.annotation.AgAttribute; +import io.agrest.annotation.AgId; import io.agrest.annotation.AgRelationship; import io.agrest.compiler.AgEntityCompiler; import io.agrest.compiler.AnnotationsAgEntityCompiler; @@ -36,6 +37,17 @@ public void testGetOrCreate_Attribute() { assertSame(pd, cache.getOrCreate("name")); } + @Test + public void testGetOrCreate_Id() { + EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); + PathDescriptor pd = cache.getOrCreate("id"); + assertNotNull(pd); + assertTrue(pd.isAttributeOrId()); + assertEquals(Integer.class, pd.getType()); + assertEquals("id", pd.getPathExp().getPath()); + assertSame(pd, cache.getOrCreate("id")); + } + @Test public void testGetOrCreate_Relationship() { EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); @@ -58,6 +70,17 @@ public void testGetOrCreate_RelatedAttribute() { assertSame(pd, cache.getOrCreate("y.name")); } + @Test + public void testGetOrCreate_RelatedId() { + EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); + PathDescriptor pd = cache.getOrCreate("y.id"); + assertNotNull(pd); + assertTrue(pd.isAttributeOrId()); + assertEquals(Integer.class, pd.getType()); + assertEquals("y.id", pd.getPathExp().getPath()); + assertSame(pd, cache.getOrCreate("y.id")); + } + @Test public void tesGetOrCreate_BadPath() { EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); @@ -78,6 +101,11 @@ public void testGetOrCreate_OuterRelatedAttribute() { public static class X { + @AgId + public Integer getId() { + throw new UnsupportedOperationException(); + } + @AgAttribute public String getName() { throw new UnsupportedOperationException(); @@ -94,5 +122,10 @@ public static class Y { public String getName() { throw new UnsupportedOperationException(); } + + @AgId + public Integer getId() { + throw new UnsupportedOperationException(); + } } }