论坛首页 入门技术论坛

HQL语句传入传入日期问题,试验了好几种方式,都不成功,麻烦个位高手看下咯

浏览 4487 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-04-19  
测试代码如下
1,测试实体类
package com.dtsoft;

import java.util.Date;

/**
* Test generated by MyEclipse Persistence Tools
*/

public class Test implements java.io.Serializable {

// Fields

private Integer id;

private Date dt;

// Constructors

/** default constructor */
public Test() {
}

/** minimal constructor */
public Test(Integer id) {
this.id = id;
}

/** full constructor */
public Test(Integer id, Date dt) {
this.id = id;
this.dt = dt;
}

// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Date getDt() {
return this.dt;
}

public void setDt(Date dt) {
this.dt = dt;
}

}
2测试代码
1:第一种方法
package com.dtsoft.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Configuration cf = new Configuration().configure();
SessionFactory sf = cf.buildSessionFactory();
Date date = new Date();
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.set(Calendar.HOUR_OF_DAY, 00);
rightNow.set(Calendar.MINUTE, 00);
rightNow.set(Calendar.SECOND, 00);
Date date1 = rightNow.getTime();

System.out.println(date);
Session s = sf.openSession();
String hql = "select * from text where dt<'"+date1+"'";
Transaction t = s.getTransaction();
t.begin();
s.createQuery(hql);

t.commit();
s.flush();
s.close();
}
报以下异常:
Mon Feb 07 03:12:40 CST 2000
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from text where dt<'Mon Feb 07 00:00:00 CST 2000']
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:155)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.dtsoft.test.Test.main(Test.java:49)
Caused by: line 1:8: unexpected token: *
at org.hibernate.hql.antlr.HqlBaseParser.selectClause(HqlBaseParser.java:1330)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1106)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:238)
... 9 more

2:一种方法

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Configuration cf = new Configuration().configure();
SessionFactory sf = cf.buildSessionFactory();

Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.set(Calendar.HOUR_OF_DAY, 00);
rightNow.set(Calendar.MINUTE, 00);
rightNow.set(Calendar.SECOND, 00);
Date date1 = rightNow.getTime();

System.out.println(date);
Session s = sf.openSession();
String hql = "select * from text where dt<?";

Transaction t = s.getTransaction();
t.begin();

s.createQuery(hql).setDate(0, date1);

t.commit();
s.flush();
s.close();
}
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from text where dt<?]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:155)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.dtsoft.test.Test.main(Test.java:49)
Caused by: line 1:8: unexpected token: *
at org.hibernate.hql.antlr.HqlBaseParser.selectClause(HqlBaseParser.java:1330)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1106)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:238)
... 9 more
   发表时间:2008-04-19  
3:第三中方法
package com.dtsoft.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Configuration cf = new Configuration().configure();
SessionFactory sf = cf.buildSessionFactory();
String time = "2007-08-09 09:06:03";

Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.set(Calendar.HOUR_OF_DAY, 00);
rightNow.set(Calendar.MINUTE, 00);
rightNow.set(Calendar.SECOND, 00);
Date date1 = rightNow.getTime();

System.out.println(date);
Session s = sf.openSession();
String hql = "select * from text where to_char(dt,'YYYY-MM-DD HH24:MI:SS')<'"+time+"'";

Transaction t = s.getTransaction();
t.begin();

s.createQuery(hql);

t.commit();
s.flush();
s.close();
}

}
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from text where to_char(dt,'YYYY-MM-DD HH24:MI:SS')<'2007-08-09 09:06:03']
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:155)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.dtsoft.test.Test.main(Test.java:49)
Caused by: line 1:8: unexpected token: *
at org.hibernate.hql.antlr.HqlBaseParser.selectClause(HqlBaseParser.java:1330)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1106)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:238)
... 9 more
0 请登录后投票
   发表时间:2008-04-19  
忘了加一句
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sd.format(date);
结果也不可以
0 请登录后投票
   发表时间:2008-04-19  
你只有第二种是唯一最接近正确的写法。你看清异常信息* near line 1, column 8这说明是在第一行第八个字符处出现问题。
你应该的写法是
select text from text where dt < ?
然后setDate
HQL不是SQL,不要乱用。你用的是HQL,不是SQL。这一点要绝对牢记。你操作的是对象不是数据库表。
0 请登录后投票
   发表时间:2008-04-19  
魔力猫咪 写道
你只有第二种是唯一最接近正确的写法。你看清异常信息* near line 1, column 8这说明是在第一行第八个字符处出现问题。
你应该的写法是
select text from text where dt < ?
然后setDate
HQL不是SQL,不要乱用。你用的是HQL,不是SQL。这一点要绝对牢记。你操作的是对象不是数据库表。

嘿嘿,粗心,但这样写HQL好像支持吧,还有是公司里把hib全封装了,根本无法调用第2中方法去做,能直接在HQL中传入date吗
0 请登录后投票
   发表时间:2008-04-19  
javaeye高手们,知道就说出来了,评东西就来劲,做程序员实在点,能解决就帮忙打几行字吗?别那么吝啬,当评委有么子意思啊,看了帖也不帮忙解决
0 请登录后投票
   发表时间:2008-04-27  
时间和数据库里的对应,如果数据库内的是'YYYY-MM-DD HH:MI:SS'传入的就应该这样,如果数据库里的只是是YYYY-MM-DD,那么传入的也应该这样。你的HH24我觉得应该HH就好了。比如2008-04-26 14:30:00
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics