Skip to content

Commit f45fe8f

Browse files
committed
[#1232] Fix JPABinder to bind multiple ids
1 parent 7a4e4d7 commit f45fe8f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

framework/src/play/db/jpa/JPAPlugin.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,34 @@ public Object bind(RootParamNode rootParamNode, String name, Class clazz, java.l
4747

4848
ParamNode paramNode = rootParamNode.getChild(name, true);
4949

50-
String keyName = Model.Manager.factoryFor(clazz).keyName();
51-
String[] ids = paramNode.getChild(keyName, true).getValues();
50+
String[] keyNames = new JPAModelLoader(clazz).keyNames();
51+
ParamNode[] ids = new ParamNode[keyNames.length];
52+
// Collect the matching ids
53+
int i = 0;
54+
for (String keyName : keyNames) {
55+
ids[i++] = paramNode.getChild(keyName, true);
56+
}
5257
if (ids != null && ids.length > 0) {
5358
try {
54-
Query query = JPA.em().createQuery("from " + clazz.getName() + " o where o." + keyName + " = ?");
59+
EntityManager em = JPA.em();
60+
String q = "from " + clazz.getName() + " o where";
61+
for (String keyName : keyNames) {
62+
q += " o." + keyName + " = ? and " ;
63+
}
64+
if (q.length() > 4) {
65+
q = q.substring(0, q.length() - 4);
66+
}
67+
Query query = em.createQuery(q);
5568
// The primary key can be a composite.
56-
int i = 1;
57-
Class pk = Model.Manager.factoryFor(clazz).keyType();
58-
for (String id : ids) {
59-
query.setParameter(i++, Binder.directBind(rootParamNode.getOriginalKey(), annotations, id, pk, null));
69+
Class[] pk = new JPAModelLoader(clazz).keyTypes();
70+
int j = 0;
71+
for (ParamNode id : ids) {
72+
if (id.getValues() == null || id.getValues().length == 0) {
73+
// We have no ids, it is a new entity
74+
return GenericModel.create(rootParamNode, name, clazz, annotations);
75+
}
76+
query.setParameter(j + 1, Binder.directBind(id.getOriginalKey(), annotations, id.getValues()[0], pk[j++], null));
77+
6078
}
6179
Object o = query.getSingleResult();
6280
return GenericModel.edit(rootParamNode, name, o, annotations);

samples-and-tests/just-test-cases/test/YamlTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public void testYamlLoading() {
1010
Fixtures.load("yamlTestData.yml");
1111
YamlModel ym = YamlModel.all().first();
1212
assertEquals("Morten", ym.name);
13-
13+
14+
assertEquals(DataWithCompositeKey.all().fetch().size(), 2);
15+
1416
//check binary
1517
assertEquals("This String is stored in yaml file using base64", new String(ym.binaryData));
1618

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
YamlModel(ym1):
22
id: 1
33
name: Morten
4-
binaryData: !!binary VGhpcyBTdHJpbmcgaXMgc3RvcmVkIGluIHlhbWwgZmlsZSB1c2luZyBiYXNlNjQ=
4+
binaryData: !!binary VGhpcyBTdHJpbmcgaXMgc3RvcmVkIGluIHlhbWwgZmlsZSB1c2luZyBiYXNlNjQ=
5+
6+
DataWithCompositeKey(1):
7+
key1: a
8+
key2: b
9+
10+
DataWithCompositeKey(2):
11+
key1: a
12+
key2: c

0 commit comments

Comments
 (0)