0

I am using oracle database and spring ibatis.

I have a query which will return two results from that i need to get a record which has minimum value on a column.

In Oracle i do that using below query:

SELECT * 
FROM ANUMBER$ROOT ROOT 
WHERE ROOT.ROOT_NUMBER=1546305 
AND ROOT.MOL_WEIGHT = (SELECT MIN(MOL_WEIGHT) 
                       FROM ANUMBER$ROOT 
                       WHERE ROOT_NUMBER=1546305);

I have converted this query to like below in spring ibatis

SELECT * 
FROM ANUMBER$ROOT ROOT 
WHEREe ROOT.ROOT_NUMBER= #value# 
AND ROOT.MOL_WEIGHT = (SELECT MIN(MOL_WEIGHT) 
                       FROM ANUMBER$ROOT 
                       WHERE ROOT_NUMBER= #value#);

But this throws me below error:

**log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in abbott/gprd/compoundInfo/dao/ibatis/LibraCompoundInformationLookup.xml.

--- The error occurred while preparing the mapped statement for execution.
--- Check the selectCompoundInfoByRootNumber.
--- Check the SQL statement.
--- Cause: java.util.NoSuchElementException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in abbott/gprd/compoundInfo/dao/ibatis/LibraCompoundInformationLookup.xml.

--- The error occurred while preparing the mapped statement for execution.
--- Check the selectCompoundInfoByRootNumber.
--- Check the SQL statement.
--- Cause: java.util.NoSuchElementException Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in abbott/gprd/compoundInfo/dao/ibatis/LibraCompoundInformationLookup.xml.

--- The error occurred while preparing the mapped statement for execution.
--- Check the selectCompoundInfoByRootNumber.
--- Check the SQL statement.
--- Cause: java.util.NoSuchElementException at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188) at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:104) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:566) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:541) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106) at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:243) at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193) at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:241) at abbott.gprd.compoundInfo.dao.ibatis.CompoundInformationDao.getCompoundInformationForRootNumber(CompoundInformationDao.java:66) at abbott.gprd.compoundInfo.dao.ibatis.CompoundInformationDao.main(CompoundInformationDao.java:183) Caused by: java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:332) at com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql.processDynamicElements(SimpleDynamicSql.java:90) at com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql.getSql(SimpleDynamicSql.java:45) at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:168) ... 9 more ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]**

Can anyone please help me in converting this query from oracle to spring ibatis?

pablomatico
  • 2,222
  • 20
  • 25
Prakash
  • 139
  • 1
  • 9

1 Answers1

0

Too long for a comment:

You can find the row(s) with the minimum value for a given column using the RANK (or DENSE_RANK) analytic function (and this does not need the extra correlated query):

SELECT *
FROM   (
  SELECT root.*,
         RANK() OVER ( ORDER BY MOL_WEIGHT ) AS rnk
  FROM   ANUMBER$ROOT ROOT 
  WHERE  ROOT_NUMBER=1546305
)
WHERE rnk = 1;
MT0
  • 143,790
  • 11
  • 59
  • 117
  • Thanks for your reply.But the problem the table name contain another $ when used in java like **ANUMBER$$ROOT** – Prakash Aug 26 '16 at 15:58