From 13864ba8b62228c4d3ca90adbc0d49bf38a78dc2 Mon Sep 17 00:00:00 2001 From: Marcel May Date: Sat, 21 Apr 2012 23:45:45 +0200 Subject: [PATCH] [#1502] JPA/Hibernate warns about use of deprecated positional parameter - Fixes JPA parameter binding by replacing '?' with '?1' (or whatever the index) - Replaced query string catenation with StringBuilder --- framework/src/play/db/jpa/GenericModel.java | 4 +- framework/src/play/db/jpa/JPAPlugin.java | 12 +++--- framework/src/play/db/jpa/JPQL.java | 44 ++++++++++----------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/framework/src/play/db/jpa/GenericModel.java b/framework/src/play/db/jpa/GenericModel.java index 1203420543..25d48ef4db 100644 --- a/framework/src/play/db/jpa/GenericModel.java +++ b/framework/src/play/db/jpa/GenericModel.java @@ -114,7 +114,7 @@ public static T edit(ParamNode rootParamNode, String name, O if (_id.equals("")) { continue; } - Query q = em.createQuery("from " + relation + " where " + keyName + " = ?"); + Query q = em.createQuery("from " + relation + " where " + keyName + " = ?1"); q.setParameter(1, Binder.directBind(rootParamNode.getOriginalKey(), annotations,_id, Model.Manager.factoryFor((Class) Play.classloader.loadClass(relation)).keyType(), null)); try { l.add(q.getSingleResult()); @@ -129,7 +129,7 @@ public static T edit(ParamNode rootParamNode, String name, O String[] ids = fieldParamNode.getChild(keyName, true).getValues(); if (ids != null && ids.length > 0 && !ids[0].equals("")) { - Query q = em.createQuery("from " + relation + " where " + keyName + " = ?"); + Query q = em.createQuery("from " + relation + " where " + keyName + " = ?1"); q.setParameter(1, Binder.directBind(rootParamNode.getOriginalKey(), annotations, ids[0], Model.Manager.factoryFor((Class) Play.classloader.loadClass(relation)).keyType(), null)); try { Object to = q.getSingleResult(); diff --git a/framework/src/play/db/jpa/JPAPlugin.java b/framework/src/play/db/jpa/JPAPlugin.java index 321afb5076..804ddae11d 100644 --- a/framework/src/play/db/jpa/JPAPlugin.java +++ b/framework/src/play/db/jpa/JPAPlugin.java @@ -3,13 +3,11 @@ import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Level; -import org.apache.log4j.config.PropertyGetter; import org.hibernate.CallbackException; import org.hibernate.EmptyInterceptor; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.ejb.Ejb3Configuration; import org.hibernate.type.Type; -import play.Invoker.InvocationContext; import play.Logger; import play.Play; import play.PlayPlugin; @@ -71,14 +69,16 @@ public Object bind(RootParamNode rootParamNode, String name, Class clazz, java.l if (ids != null && ids.length > 0) { try { EntityManager em = JPABase.getJPAConfig(clazz).getJPAContext().em(); - String q = "from " + clazz.getName() + " o where"; + StringBuilder q = new StringBuilder().append("from ").append(clazz.getName()).append(" o where"); + int keyIdx = 1; for (String keyName : keyNames) { - q += " o." + keyName + " = ? and " ; + q.append(" o.").append(keyName).append(" = ?").append(keyIdx).append(" and "); + keyIdx++; } if (q.length() > 4) { - q = q.substring(0, q.length() - 4); + q = q.delete(q.length() - 4, q.length()); } - Query query = em.createQuery(q); + Query query = em.createQuery(q.toString()); // The primary key can be a composite. Class[] pk = new JPAModelLoader(clazz).keyTypes(); int j = 0; diff --git a/framework/src/play/db/jpa/JPQL.java b/framework/src/play/db/jpa/JPQL.java index 1bfd532eac..b1aa975ebe 100644 --- a/framework/src/play/db/jpa/JPQL.java +++ b/framework/src/play/db/jpa/JPQL.java @@ -131,10 +131,10 @@ public String createFindByQuery(String entityName, String entityClass, String qu if (query.trim().toLowerCase().startsWith("order by ")) { return "from " + entityName + " " + query; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params != null && params.length == 1) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params != null && params.length == 1) { query += " = ?1"; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params == null) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params == null) { query += " = null"; } return "from " + entityName + " where " + query; @@ -150,10 +150,10 @@ public String createDeleteQuery(String entityName, String entityClass, String qu if (query.trim().toLowerCase().startsWith("from ")) { return "delete " + query; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params != null && params.length == 1) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params != null && params.length == 1) { query += " = ?1"; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params == null) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params == null) { query += " = null"; } return "delete from " + entityName + " where " + query; @@ -172,10 +172,10 @@ public String createCountQuery(String entityName, String entityClass, String que if (query.trim().toLowerCase().startsWith("order by ")) { return "select count(*) from " + entityName; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params != null && params.length == 1) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params != null && params.length == 1) { query += " = ?1"; } - if (query.trim().indexOf(" ") == -1 && query.trim().indexOf("=") == -1 && params == null) { + if (query.trim().indexOf(' ') == -1 && query.trim().indexOf('=') == -1 && params == null) { query += " = null"; } if (query.trim().length() == 0) { @@ -210,7 +210,7 @@ public Query bindParameters(Query q, Map params) { public String findByToJPQL(String findBy) { findBy = findBy.substring(2); - StringBuffer jpql = new StringBuffer(); + StringBuilder jpql = new StringBuilder(); String subRequest; if (findBy.contains("OrderBy")) subRequest = findBy.split("OrderBy")[0]; @@ -220,52 +220,52 @@ public String findByToJPQL(String findBy) { String part = parts[i]; if (part.endsWith("NotEqual")) { String prop = extractProp(part, "NotEqual"); - jpql.append(prop + " <> ?"); + jpql.append(prop).append(" <> ?").append(i + 1); } else if (part.endsWith("Equal")) { String prop = extractProp(part, "Equal"); - jpql.append(prop + " = ?"); + jpql.append(prop).append(" = ?").append(i + 1); } else if (part.endsWith("IsNotNull")) { String prop = extractProp(part, "IsNotNull"); - jpql.append(prop + " is not null"); + jpql.append(prop).append(" is not null"); } else if (part.endsWith("IsNull")) { String prop = extractProp(part, "IsNull"); - jpql.append(prop + " is null"); + jpql.append(prop).append(" is null"); } else if (part.endsWith("LessThan")) { String prop = extractProp(part, "LessThan"); - jpql.append(prop + " < ?"); + jpql.append(prop).append(" < ?").append(i + 1); } else if (part.endsWith("LessThanEquals")) { String prop = extractProp(part, "LessThanEquals"); - jpql.append(prop + " <= ?"); + jpql.append(prop).append(" <= ?").append(i + 1); } else if (part.endsWith("GreaterThan")) { String prop = extractProp(part, "GreaterThan"); - jpql.append(prop + " > ?"); + jpql.append(prop).append(" > ?").append(i + 1); } else if (part.endsWith("GreaterThanEquals")) { String prop = extractProp(part, "GreaterThanEquals"); - jpql.append(prop + " >= ?"); + jpql.append(prop).append(" >= ?").append(i + 1); } else if (part.endsWith("Between")) { String prop = extractProp(part, "Between"); - jpql.append(prop + " < ? AND " + prop + " > ?"); + jpql.append(prop).append(" < ?").append(i + 1).append(" AND ").append(prop).append(" > ?").append(i + 1); } else if (part.endsWith("Like")) { String prop = extractProp(part, "Like"); // HSQL -> LCASE, all other dbs lower if (isHSQL()) { - jpql.append("LCASE(" + prop + ") like ?"); + jpql.append("LCASE(").append(prop).append(") like ?").append(i + 1); } else { - jpql.append("LOWER(" + prop + ") like ?"); + jpql.append("LOWER(").append(prop).append(") like ?").append(i + 1); } } else if (part.endsWith("Ilike")) { String prop = extractProp(part, "Ilike"); if (isHSQL()) { - jpql.append("LCASE(" + prop + ") like LCASE(?)"); + jpql.append("LCASE(").append(prop).append(") like LCASE(?").append(i + 1).append(")"); } else { - jpql.append("LOWER(" + prop + ") like LOWER(?)"); + jpql.append("LOWER(").append(prop).append(") like LOWER(?").append(i + 1).append(")"); } } else if (part.endsWith("Elike")) { String prop = extractProp(part, "Elike"); - jpql.append(prop + " like ?"); + jpql.append(prop).append(" like ?").append(i + 1); } else { String prop = extractProp(part, ""); - jpql.append(prop + " = ?"); + jpql.append(prop).append(" = ?").append(i + 1); } if (i < parts.length - 1) { jpql.append(" AND ");