Skip to content

Commit 39365f3

Browse files
committed
Added fix to ModelBindParamTransformer to not query a parameter multiple times in the same request
1 parent 7ea62a6 commit 39365f3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/main/java/org/javawebstack/framework/bind/ModelBindParamTransformer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.javawebstack.orm.ORM;
66
import org.javawebstack.orm.Repo;
77

8+
import java.util.HashMap;
9+
import java.util.Map;
810
import java.util.UUID;
911

1012
public class ModelBindParamTransformer extends DefaultRouteParamTransformer {
@@ -14,7 +16,22 @@ public class ModelBindParamTransformer extends DefaultRouteParamTransformer {
1416

1517
public ModelBindParamTransformer() {
1618
super();
17-
this.transformer = (exchange, repo, fieldName, source) -> repo.accessible(accessorAttribName == null ? null : exchange.attrib(accessorAttribName)).where(fieldName, source).first();
19+
this.transformer = (exchange, repo, fieldName, source) -> {
20+
Map<Class<? extends Model>, Map<Object, Object>> cache = exchange.attrib("__modelbindcache__");
21+
if(cache == null) {
22+
cache = new HashMap<>();
23+
exchange.attrib("__modelbindcache__", cache);
24+
}
25+
if(!cache.containsKey(repo.getInfo().getModelClass()))
26+
cache.put(repo.getInfo().getModelClass(), new HashMap<>());
27+
Map<Object, Object> modelCache = cache.get(repo.getInfo().getModelClass());
28+
Object model = modelCache.get(source);
29+
if(model == null) {
30+
model = repo.accessible(accessorAttribName == null ? null : exchange.attrib(accessorAttribName)).where(fieldName, source).first();
31+
modelCache.put(source, model);
32+
}
33+
return model;
34+
};
1835
for (Class<? extends Model> model : ORM.getModels()) {
1936
ModelBind[] binds = model.getDeclaredAnnotationsByType(ModelBind.class);
2037
if (binds.length == 0)

0 commit comments

Comments
 (0)