用hibernate查询,用News类来保存查询结果,有Timestamp类型时QuerySyntaxException?

J2EE 码拜 9年前 (2015-08-12) 1494次浏览

我的hql语句是这样写的

String hql = "select new News(n.newsId,n.newsTitle,n.newsTime,n.newsPic) from News n order by n.newsTime desc";

这样查出来会报错,说没有适合的构造函数,但是我写了对应的构造函数了。

我的数据库是mysql5.5,数据库中这个newsTime是timestamp类型的;然后用myeclipse生成的映射文件中,newsTime是timestamp类型的。
如果我直接将hql语句写成  from News,或者new News()中不包含这个newsTime,就不会报错。求高手指导
错误信息:

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [model.News] [select new News(n.newsId,n.newsTitle,n.newsTime,n.newsPic) from model.News n order by n.newsTime desc]
	at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
	at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
	at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
	at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:258)
	at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
	at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
	at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
	at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
	at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
	at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
	at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
	at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
	at dao.NewsDaoImpl.getNewsList(NewsDaoImpl.java:32)
	at service.NewsServiceImpl.getNewsList(NewsServiceImpl.java:23)
	at action.NewsAction.execute(NewsAction.java:84)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)

我的News类:

package model;
import java.sql.Timestamp;

/**
 * News entity. @author MyEclipse Persistence Tools
 */

public class News  implements java.io.Serializable {

    // Fields    
     private Integer newsId;
     private String newsTitle;
     private Timestamp newsTime;
     private Integer newsEditorId;
     private String newsEditor;
     private Integer newsPic;
     private String newsContent;


    // Constructors

    /** default constructor */
    public News() {
    }
    
    //id,標題,圖片
    public News(Integer newsId, String newsTitle, Integer newsPic) {
		this.newsId = newsId;
		this.newsTitle = newsTitle;
		this.newsPic = newsPic;
	}

    //id,標題,時間,圖片
	public News(Integer newsId, String newsTitle, Timestamp newsTime,
			Integer newsPic) {
		this.newsId = newsId;
		this.newsTitle = newsTitle;
		this.newsTime = newsTime;
		this.newsPic = newsPic;
	}

	/** full constructor */
    public News(String newsTitle, Timestamp newsTime, Integer newsEditorId, String newsEditor, Integer newsPic, String newsContent) {
        this.newsTitle = newsTitle;
        this.newsTime = newsTime;
        this.newsEditorId = newsEditorId;
        this.newsEditor = newsEditor;
        this.newsPic = newsPic;
        this.newsContent = newsContent;
    }
   
    // Property accessors

    public Integer getNewsId() {
        return this.newsId;
    }
    
    public void setNewsId(Integer newsId) {
        this.newsId = newsId;
    }

    public String getNewsTitle() {
        return this.newsTitle;
    }
    
    public void setNewsTitle(String newsTitle) {
        this.newsTitle = newsTitle;
    }

    public Timestamp getNewsTime() {
        return this.newsTime;
    }
    
    public void setNewsTime(Timestamp newsTime) {
        this.newsTime = newsTime;
    }

    public Integer getNewsEditorId() {
        return this.newsEditorId;
    }
    
    public void setNewsEditorId(Integer newsEditorId) {
        this.newsEditorId = newsEditorId;
    }

    public String getNewsEditor() {
        return this.newsEditor;
    }
    
    public void setNewsEditor(String newsEditor) {
        this.newsEditor = newsEditor;
    }

    public Integer getNewsPic() {
        return this.newsPic;
    }
    
    public void setNewsPic(Integer newsPic) {
        this.newsPic = newsPic;
    }

    public String getNewsContent() {
        return this.newsContent;
    }
    
    public void setNewsContent(String newsContent) {
        this.newsContent = newsContent;
    }

}
#1
new 后面加个实体对象这样没用过,我记得以前这样用过
select new map(n.newsId,n.newsTitle,n.newsTime,n.newsPic)这样看行不行
#2
select 的里面这个 感觉怪怪的,能直接这样写?new News(n.newsId,n.newsTitle,n.newsTime,n.newsPic)
#3

20分

 private Timestamp newsTime;
修改为
private java.util.Date newsTime试验一下?

另外,我写了个代码来测试你说的问题,我的hibernate会报如下错误,为虾米你的没有? 你的hibernate版本太老? 有下面的错误提示能更快速的定位问题
 Unable to locate appropriate constructor on class [bean.ContactBean]. Expected arguments are: java.lang.String, java.util.Date

#4

回复3楼:

改了类型,还是无法解决,最终我放弃了这种酷炫的hql写法。哎
最终代码是

			String hql = "select newsId,newsTitle,newsTime,newsPic from News order by newsTime desc";
			List<Object[]> list = session.createQuery(hql)
					.setFirstResult(fristResult).setMaxResults(maxResult)
					.list();
			
			newsList = new ArrayList<News>();
			
			for (Object[] object : list) {
				News n = new News();
				n.setNewsId((Integer) object[0]);
				n.setNewsTitle((String) object[1]);
				n.setNewsTime((Timestamp) object[2]);
				n.setNewsPic((Integer) object[3]);
				newsList.add(n);
			}

但,原来的那个为啥不能用,还是不知道。不甘心啊

#5

回复4楼:

那个写法没有问题,我亲测可用。你升级一下hibernate,看看是不是想哦这样报错:明确的说需要什么样Constructor。

#6

回复5楼:

恩,确实是我的hibernate版本太老,升级到最新的之后,就不报错了。
谢谢啦~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用hibernate查询,用News类来保存查询结果,有Timestamp类型时QuerySyntaxException?
喜欢 (0)
[1034331897@qq.com]
分享 (0)