我正在尝试获取与月份列完全匹配的记录(这是id列)但我没有得到匹配记录。我在sql db中执行了这样的查询“从webSheet中选择表单month ='Feb'“它被执行。在HQL中它不起作用..
我的堆栈流程:
INFO: table found: soft.dbo.employee
SHEET DAO CLASS
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
Retrieve Sheet:::::::Feb
INFO: columns: [empid, time, empname]
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [pk__employee__73a521ea]
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: table found: soft`enter code here`.dbo.webSheet
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: columns: [month, sheet]
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [pk__websheet__69279377]
Nov 18, 2011 12:57:12 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
Hibernate: select websheet0_.sheet as col_0_0_ from webSheet websheet0_ where websheet0_.month=Feb
Error-->could not execute query
In opensheet class-->result
Nov 18, 2011 12:57:13 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 207, SQLState: S0001
Nov 18, 2011 12:57:13 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Invalid column name 'Feb'.
我的代码如下:DataAccessDAO.java
try{
Session session=sessionFactory.openSession();
WebSheet ws=new WebSheet();
ws.setMonth(month);
txn=session.beginTransaction();
query=session.createQuery("select ws.sheet from WebSheet as ws where ws.month="+month).list();
System.out.println("result::::::::::"+query.size());
String result=query.iterator().next().getSheet();
}
catch(Exception ex){
System.err.println("Error-->"+ex.getMessage());
if(txn!=null){
txn.rollback();
}
}
我的映射文件:
<hibernate-mapping>
<class name="com.MyApp.beans.WebSheet" table="webSheet">
<id name="month" type="string">
<generator class="assigned"/>
</id>
<property name="sheet" type="string"/>
</class>
</hibernate-mapping>
我的Pojo课程:
public class WebSheet {
private String month;
private String sheet;
/*setters and getters*****
答案 0 :(得分:5)
查询应该是这样的 -
Query query = session.createQuery("from com.MyApp.beans.WebSheet as ws where ws.month=:mth");
query.setParameter("mth", month);
WebSheet wsObj = null;
String sheet = null;
Iterator it = query.iterate(); it.hasNext();{
wsObj = (WebSheet) it.next();
}
if(wsObj != null){
sheet = wsObj.getSheet();
}
session.getTransaction().commit();
答案 1 :(得分:0)
正确的方式是@anirudhamundada说。
但是如果你想继续做错和不安全,你必须添加:
query=session.createQuery("select ws.sheet from WebSheet as ws where ws.month='"+month + "'").list();
您似乎没有返回String
。
尝试
Object o = query.iterator().next().getSheet();
System.out.println(o.getClass().getName());
什么类型?