I am getting an exception running the following query. It appears to be related to the fact that MySQL Dialect is using an underscore as the schema seperator (In this case nmcibsa is the schema and SEAT_INFO is the table name:
HQL:
select count(seatInfo) from SeatInfo as seatInfo where seatInfo.seatStatus=:seatStatus
Generated SQL:
Hibernate: select count(seatinfo0_.ID) as x0_0_ from nmcibsa_SEAT_INFO seatinfo0_ where (seatinfo0_.SEAT_STATUS=? )
Stack Trace(Only down to start of hibernate code to save space):
Feb 18, 2004 3:43:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1146, SQLState: S1000
Feb 18, 2004 3:43:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: General error, message from server: "Table 'nmcibsa.nmcibsa_seat_info' doesn't exist"
Feb 18, 2004 3:43:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1146, SQLState: S1000
Feb 18, 2004 3:43:07 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: General error, message from server: "Table 'nmcibsa.nmcibsa_seat_info' doesn't exist"
Feb 18, 2004 3:43:07 PM net.sf.hibernate.JDBCException <init>
SEVERE: Could not execute query
java.sql.SQLException: General error, message from server: "Table 'nmcibsa.nmcibsa_seat_info' doesn't exist"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1538)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:794)
at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:846)
at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1540)
at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:27)
Hibernate Version: 2.1.2
mapping document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<meta attribute="extends">Persistent</meta>
<meta attribute="session-method">
mil.spawar.bsa.dataaccess.hibernate.HibernateSession.currentSession();
</meta>
<class name="mil.spawar.bsa.businessobjects.hibernate.SeatInfo" schema="nmcibsa" table="SEAT_INFO">
<cache usage="read-write"/>
<id name="id" type="java.lang.Long">
<column name="ID" not-null="true"/>
<!-- Use for MySQL
-->
<generator class="native"/>
<!-- Use for Oracle
<generator class="sequence">
<param name="sequence">SEAT_INFO_SEQUENCE</param>
</generator>
-->
</id>
<property name="seatId" type="java.lang.Long">
<meta attribute="finder">findBySeatId</meta>
<column name="SEAT_ID" length="7" not-null="true"/>
</property>
<property name="seatNumber" type="java.lang.Long">
<meta attribute="finder">findBySeatNbr</meta>
<column name="SEAT_NBR" length="7" not-null="true"/>
</property>
<property name="seatStatus" type="string">
<meta attribute="finder">findBySeatStatus</meta>
<column name="SEAT_STATUS" length="1"/>
</property>
<property name="seatOrgCode" type="string">
<meta attribute="finder">findBySeatOrgCode</meta>
<column name="SEAT_ORGCODE" length="25"/>
</property>
<property name="seatName" type="string">
<meta attribute="finder">findBySeatName</meta>
<column name="SEAT_NAME" length="50"/>
</property>
<property name="seatClin" type="string">
<meta attribute="finder">findBySeatClin</meta>
<column name="SEAT_CLIN" length="6"/>
</property>
<property name="seatType" type="string">
<meta attribute="finder">findBySeatType</meta>
<column name="SEAT_TYPE" length="1"/>
</property>
<property name="seatFee" type="java.lang.Double">
<meta attribute="finder">findBySeatFee</meta>
<column name="SEAT_FEE" sql-type="NUMERIC(9, 2)"/>
</property>
<property name="supportFee" type="java.lang.Double">
<meta attribute="finder">findBySupportFee</meta>
<column name="SUPPORT_FEE" sql-type="NUMERIC(9, 2)"/>
</property>
<property name="seatChargeJon" type="string">
<meta attribute="finder">findBySeatChargeJon</meta>
<column name="SEAT_CHARGE_JON" length="10"/>
</property>
<property name="seatErrorJon" type="string">
<meta attribute="finder">findBySeatErrorJon</meta>
<column name="SEAT_ERROR_JON" length="10"/>
</property>
<property name="auditUserId" type="string">
<meta attribute="finder">findByAuditUserId</meta>
<column name="AUDIT_USER_ID" length="75"/>
</property>
<property name="auditDate" type="timestamp">
<meta attribute="finder">findByAuditDate</meta>
<column name="AUDIT_DATE"/>
</property>
<set name="nmciFeesHistory" order-by="id asc" inverse="true" lazy="true">
<key column="SEATINFO_ID"/>
<one-to-many class="mil.spawar.bsa.businessobjects.hibernate.NmciFeesHistory"/>
</set>
</class>
</hibernate-mapping>
Code:
public static int countBy(SeatInfo seatInfo, String operator) throws SQLException, HibernateException {
Session session = mil.spawar.bsa.dataaccess.hibernate.HibernateSession.currentSession();
StringBuffer queryBuffer = new StringBuffer();
NmciFeesHistory history = null;
/*--------------------------------------------------------------------*
* Create appropriate select clause and append join alias for *
* component object properties that are not null *
*--------------------------------------------------------------------*/
if (seatInfo.getNmciFeesHistory() != null && seatInfo.getNmciFeesHistory().size() > 0) {
history = (NmciFeesHistory) seatInfo.getNmciFeesHistory().iterator().next();
queryBuffer.append(
"select count(seatInfo) from NmciFeesHistory as history " + "inner join history.seatInfo as seatInfo ");
} else {
queryBuffer.append("select count(seatInfo) from SeatInfo as seatInfo ");
}
String whereClause = buildWhereClause(seatInfo, operator);
if (StringUtils.substringAfter(whereClause, "where").length() > 1)
queryBuffer.append(whereClause);
else
logger.severe("where clause empty for this query");
Query query = session.createQuery(queryBuffer.toString());
/*--------------------------------------------------------------------*
* Set query parameters. *
*--------------------------------------------------------------------*/
query.setProperties(seatInfo);
if (history != null) {
query.setProperties(history);
}
Iterator iter = query.iterate();
Integer count = (Integer) iter.next();
return count.intValue();
}
stack trace above
database and version: MySQL version 4
|