hibernate bug fixed and related to a join tables that have some columns with identical name. Bug resolved using JDBC. ConnectionManager and DatabaseManagement classes modified adding the method "executeQueryJDBC" in ConnectionManager, used only for the submitquery operation
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@99393 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
ecee3f5fdf
commit
1e191ee9f3
|
@ -3,6 +3,12 @@ package org.gcube.dataaccess.databases.utils;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
@ -118,6 +124,56 @@ public class ConnectionManager {
|
|||
|
||||
}
|
||||
|
||||
/** Method that execute a query */
|
||||
public List<Object> executeQueryJDBC(String query, Connection conn)
|
||||
throws Exception {
|
||||
|
||||
List<Object> result = null;
|
||||
|
||||
|
||||
// Get a statement from the connection
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
// Execute the query
|
||||
ResultSet rs = stmt.executeQuery(query);
|
||||
|
||||
//get columns
|
||||
List<Object> rows = new ArrayList<Object>();
|
||||
while (rs.next()) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
int colNum = rs.getMetaData().getColumnCount();
|
||||
|
||||
for (int i = 1; i < colNum + 1; i++) {
|
||||
String columnName = rs.getMetaData().getColumnLabel(i);
|
||||
// System.out.println("column Name: "+columnName);
|
||||
Object columnValue = rs.getObject(i);
|
||||
// System.out.println("value: "+columnValue);
|
||||
int j = 1;
|
||||
String newcolumnName = columnName;
|
||||
//rename a column if the same name is already present
|
||||
while (map.get(newcolumnName) != null) {
|
||||
newcolumnName = columnName + "_" + j;
|
||||
j++;
|
||||
}
|
||||
map.put(newcolumnName, columnValue);
|
||||
}
|
||||
rows.add(map);
|
||||
}
|
||||
|
||||
result = rows;
|
||||
|
||||
if (result == null || result.size() == 0) {
|
||||
AnalysisLogger.getLogger().debug(
|
||||
"ConnectionManager->Error: Result not available");
|
||||
throw new Exception("Result not available");
|
||||
}
|
||||
|
||||
|
||||
stmt.close();
|
||||
// conn.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Method that execute a query */
|
||||
public List<Object> executeQuery(String query,
|
||||
SessionFactory DBSessionFactory) throws Exception {
|
||||
|
@ -175,7 +231,6 @@ public class ConnectionManager {
|
|||
"In ConnectionManager-> executing query: " + query);
|
||||
|
||||
qr = ss.createSQLQuery(query);
|
||||
|
||||
|
||||
qr.setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE);
|
||||
|
||||
|
@ -211,7 +266,7 @@ public class ConnectionManager {
|
|||
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
// e.printStackTrace();
|
||||
|
||||
// System.out.println(String.format("Error while executing query: %1$s %2$s",
|
||||
// query, e.getMessage()));
|
||||
|
@ -295,26 +350,27 @@ public class ConnectionManager {
|
|||
return obj;
|
||||
|
||||
}
|
||||
|
||||
// //to cancel the execution of the current query
|
||||
// public void removeQueryExecution(SessionFactory DBSessionFactory) throws Exception{
|
||||
//
|
||||
// try{
|
||||
//
|
||||
// Session ss = DBSessionFactory.getCurrentSession();
|
||||
//
|
||||
// ss.beginTransaction();
|
||||
//
|
||||
// ss.cancelQuery();
|
||||
//
|
||||
// ss.getTransaction().commit();
|
||||
//
|
||||
// }
|
||||
// catch (Exception e) {
|
||||
// throw e;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
// //to cancel the execution of the current query
|
||||
// public void removeQueryExecution(SessionFactory DBSessionFactory) throws
|
||||
// Exception{
|
||||
//
|
||||
// try{
|
||||
//
|
||||
// Session ss = DBSessionFactory.getCurrentSession();
|
||||
//
|
||||
// ss.beginTransaction();
|
||||
//
|
||||
// ss.cancelQuery();
|
||||
//
|
||||
// ss.getTransaction().commit();
|
||||
//
|
||||
// }
|
||||
// catch (Exception e) {
|
||||
// throw e;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
// /** Method that creates the connection */
|
||||
// public SessionFactory createConnection(AlgorithmConfiguration config) {
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.io.File;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
@ -327,13 +328,13 @@ public class DatabaseManagement {
|
|||
*/
|
||||
|
||||
// method that allows to submit a query
|
||||
public List<Object> submitQuery(String query, SessionFactory session,
|
||||
public List<Object> submitQuery(String query, Connection conn,
|
||||
String pathFile) throws Exception {
|
||||
|
||||
List<Object> results = new ArrayList<Object>();
|
||||
|
||||
results = connection.executeQuery(query, session);
|
||||
|
||||
results = connection.executeQueryJDBC(query, conn);
|
||||
|
||||
if (results != null) {
|
||||
AnalysisLogger.getLogger().debug(
|
||||
"DatabaseManagement-> Query result retrieved");
|
||||
|
@ -874,9 +875,9 @@ public class DatabaseManagement {
|
|||
}
|
||||
}
|
||||
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->HEADERS: " + header);
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->HEADERS: " + header);
|
||||
|
||||
// add headers
|
||||
mapResult.put("HEADERS", header);
|
||||
|
@ -895,7 +896,7 @@ public class DatabaseManagement {
|
|||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||
((LinkedHashMap<String, Object>) element).values());
|
||||
|
||||
// // print check
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->values: " + listvalues);
|
||||
|
||||
|
@ -963,6 +964,9 @@ public class DatabaseManagement {
|
|||
// AnalysisLogger.getLogger().debug(
|
||||
// "writing the value: " + RowString + " key: "
|
||||
// + String.valueOf(i));
|
||||
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "row: " + RowString);
|
||||
|
||||
// mapResult.put(Integer.valueOf(i), RowString);
|
||||
mapResult.put(String.valueOf(i), RowString);
|
||||
|
|
Loading…
Reference in New Issue