使用hibernate查询,用News类来保存查询结果,有Timestamp类型时会出错,如何解决?

dhxwork 2015-08-05 05:05:54
我的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;
}

}
...全文
146 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ArayChou 2015-08-06
  • 打赏
  • 举报
回复
引用 4 楼 dhxwork 的回复:
但,原来的那个为啥不能用,还是不知道。。。不甘心啊
那个写法没有问题,我亲测可用。你升级一下hibernate,看看是不是想哦这样报错:明确的说需要什么样Constructor。
dhxwork 2015-08-06
  • 打赏
  • 举报
回复
引用 3 楼 ArayChou 的回复:
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
改成了Date,测试了还是原来的那个错误。。。
引用 3 楼 ArayChou 的回复:
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
改了类型,还是无法解决,最终我放弃了这种酷炫的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);
			}
但,原来的那个为啥不能用,还是不知道。。。不甘心啊
dhxwork 2015-08-06
  • 打赏
  • 举报
回复
引用 5 楼 ArayChou 的回复:
[quote=引用 4 楼 dhxwork 的回复:] 但,原来的那个为啥不能用,还是不知道。。。不甘心啊
那个写法没有问题,我亲测可用。你升级一下hibernate,看看是不是想哦这样报错:明确的说需要什么样Constructor。[/quote] 恩,确实是我的hibernate版本太老,升级到最新的之后,就不报错了。 谢谢啦~
ArayChou 2015-08-05
  • 打赏
  • 举报
回复
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
  • 打赏
  • 举报
回复
select 的里面这个 感觉怪怪的,能直接这样写?new News(n.newsId,n.newsTitle,n.newsTime,n.newsPic)
sunshiwu1984 2015-08-05
  • 打赏
  • 举报
回复
new 后面加个实体对象这样没用过,我记得以前这样用过 select new map(n.newsId,n.newsTitle,n.newsTime,n.newsPic)这样看行不行

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧