Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1502] JPA/Hibernate warns about use of deprecated positional parameter #504

Merged
merged 1 commit into from
May 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions framework/src/play/db/jpa/GenericModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static <T extends JPABase> 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<Model>) Play.classloader.loadClass(relation)).keyType(), null));
try {
l.add(q.getSingleResult());
Expand All @@ -129,7 +129,7 @@ public static <T extends JPABase> 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<Model>) Play.classloader.loadClass(relation)).keyType(), null));
try {
Object to = q.getSingleResult();
Expand Down
12 changes: 6 additions & 6 deletions framework/src/play/db/jpa/JPAPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
44 changes: 22 additions & 22 deletions framework/src/play/db/jpa/JPQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -210,7 +210,7 @@ public Query bindParameters(Query q, Map<String,Object> 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];
Expand All @@ -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 ");
Expand Down