From f45fe8f6887be6e31bc0a2e3c106002f7ae87935 Mon Sep 17 00:00:00 2001 From: Nicolas Leroux Date: Thu, 1 Dec 2011 10:22:37 +0100 Subject: [PATCH] [#1232] Fix JPABinder to bind multiple ids --- framework/src/play/db/jpa/JPAPlugin.java | 32 +++++++++++++++---- .../just-test-cases/test/YamlTest.java | 4 ++- .../just-test-cases/test/yamlTestData.yml | 10 +++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/framework/src/play/db/jpa/JPAPlugin.java b/framework/src/play/db/jpa/JPAPlugin.java index 279b00f90a..5d5602c3a5 100644 --- a/framework/src/play/db/jpa/JPAPlugin.java +++ b/framework/src/play/db/jpa/JPAPlugin.java @@ -47,16 +47,34 @@ public Object bind(RootParamNode rootParamNode, String name, Class clazz, java.l ParamNode paramNode = rootParamNode.getChild(name, true); - String keyName = Model.Manager.factoryFor(clazz).keyName(); - String[] ids = paramNode.getChild(keyName, true).getValues(); + String[] keyNames = new JPAModelLoader(clazz).keyNames(); + ParamNode[] ids = new ParamNode[keyNames.length]; + // Collect the matching ids + int i = 0; + for (String keyName : keyNames) { + ids[i++] = paramNode.getChild(keyName, true); + } if (ids != null && ids.length > 0) { try { - Query query = JPA.em().createQuery("from " + clazz.getName() + " o where o." + keyName + " = ?"); + EntityManager em = JPA.em(); + String q = "from " + clazz.getName() + " o where"; + for (String keyName : keyNames) { + q += " o." + keyName + " = ? and " ; + } + if (q.length() > 4) { + q = q.substring(0, q.length() - 4); + } + Query query = em.createQuery(q); // The primary key can be a composite. - int i = 1; - Class pk = Model.Manager.factoryFor(clazz).keyType(); - for (String id : ids) { - query.setParameter(i++, Binder.directBind(rootParamNode.getOriginalKey(), annotations, id, pk, null)); + Class[] pk = new JPAModelLoader(clazz).keyTypes(); + int j = 0; + for (ParamNode id : ids) { + if (id.getValues() == null || id.getValues().length == 0) { + // We have no ids, it is a new entity + return GenericModel.create(rootParamNode, name, clazz, annotations); + } + query.setParameter(j + 1, Binder.directBind(id.getOriginalKey(), annotations, id.getValues()[0], pk[j++], null)); + } Object o = query.getSingleResult(); return GenericModel.edit(rootParamNode, name, o, annotations); diff --git a/samples-and-tests/just-test-cases/test/YamlTest.java b/samples-and-tests/just-test-cases/test/YamlTest.java index 4874f55722..d5f8b0bf09 100644 --- a/samples-and-tests/just-test-cases/test/YamlTest.java +++ b/samples-and-tests/just-test-cases/test/YamlTest.java @@ -10,7 +10,9 @@ public void testYamlLoading() { Fixtures.load("yamlTestData.yml"); YamlModel ym = YamlModel.all().first(); assertEquals("Morten", ym.name); - + + assertEquals(DataWithCompositeKey.all().fetch().size(), 2); + //check binary assertEquals("This String is stored in yaml file using base64", new String(ym.binaryData)); diff --git a/samples-and-tests/just-test-cases/test/yamlTestData.yml b/samples-and-tests/just-test-cases/test/yamlTestData.yml index 12a7f2a184..70ed5ed692 100644 --- a/samples-and-tests/just-test-cases/test/yamlTestData.yml +++ b/samples-and-tests/just-test-cases/test/yamlTestData.yml @@ -1,4 +1,12 @@ YamlModel(ym1): id: 1 name: Morten - binaryData: !!binary VGhpcyBTdHJpbmcgaXMgc3RvcmVkIGluIHlhbWwgZmlsZSB1c2luZyBiYXNlNjQ= \ No newline at end of file + binaryData: !!binary VGhpcyBTdHJpbmcgaXMgc3RvcmVkIGluIHlhbWwgZmlsZSB1c2luZyBiYXNlNjQ= + +DataWithCompositeKey(1): + key1: a + key2: b + +DataWithCompositeKey(2): + key1: a + key2: c \ No newline at end of file