creo que he intentado todas las herramientas y el IDE de.
Al final, que se establecieron con el siguiente fragmento de código ...
El queryEditor se ejecuta como un unittest.
Suponiendo que ya tiene UnitTests, evita la molestia de configurar PersistenceContexts, DataSources, Drivers, libraries, etc ... También le permite pasar instancias de entidad como parámetros.
Y cuando haya terminado, copie query-String en una definición de @NamedParameter y configure @Test para enable = false.
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestGeneric extends AbstractModelTest {
private static final Logger logger = LoggerFactory.getLogger(TestGeneric.class.getName());
/**
* This is not a test. Just a convenience method to edit queries
*/
@Test(enabled = true)
public void queryEditor() throws Exception {
String query = "SELECT mpe " +
" FROM ActiveProduct apt JOIN apt.supportedProduct spt JOIN apt.account act JOIN act.merchantProfile mpe" +
" WHERE mpe.id = :mpeId ";
Class resultClass = MerchantProfile.class;
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("mpeId", 1L);
performQuery(query, resultClass, parameters);
}
private <T> void performQuery(String jplQuery, Class<T> type, Map parameters) throws Exception {
Query query = this.em.createQuery(jplQuery, type);
Set<Map.Entry<String, Object>> rawParameters = parameters.entrySet();
for (Map.Entry<String, Object> entry : rawParameters) {
query.setParameter(entry.getKey(), entry.getValue());
}
List<T> resultList = query.getResultList();
if (resultList.size() > 0) {
int count = 0;
StringBuffer resultString;
for (Object o : resultList) {
resultString = new StringBuffer(++count + " - ");
dumpObject(o, resultString);
logger.info(resultString.toString());
}
} else {
logger.info("Empty result list");
}
}
private void dumpObject(Object o, StringBuffer resultString) throws Exception {
if (o == null) {
resultString.append("NULL");
} else if (o instanceof Object[]) {
Object[] row = (Object[]) o;
resultString.append("[");
for (int i = 0; i < row.length; i++) {
dumpObject(row[i], resultString);
}
resultString.append("]");
} else if (o instanceof Long ||
o instanceof Double ||
o instanceof String) {
resultString.append(o.getClass().getName() + ": " + o);
} else {
resultString.append(ReflectionToStringBuilder.toString(o, ToStringStyle.SHORT_PREFIX_STYLE));
}
}
Parece que NetBeans podrían proporcionar algún tipo de soporte para este tipo de cosas en 7,0 . – Rintoul