在使用hibernate的hql对数据库进行查询时,发生如下异常,异常指出没有找到合适的构造器,原因可能如下
1、构造器的参数个数与hql中的个数是否相同
2、构造器的参数类型和hql的类型是否对应
org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.anyview.codec.pojo.response.CatalogInfo] [select new com.anyview.codec.pojo.response.CatalogInfo(t.startTime) from com.anyview.entities.SchemeContentTable as t where t.scheme.vid=? and t.status=1]
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 com.anyview.dao.impl.PkCatalogDaoImpl.cata(PkCatalogDaoImpl.java:41)
at com.anyview.dao.impl.PkCatalogDaoImpl.getCatalogs(PkCatalogDaoImpl.java:51)
at com.anyview.service.impl.PkCatalogManagerImpl.getCatalogs(PkCatalogManagerImpl.java:25)
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:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy17.getCatalogs(Unknown Source)
at com.anyview.action.PkCatalogAction.pkCatalogResponse(PkCatalogAction.java:42)
at com.anyview.server.handler.SubscribeServerHandler.channelRead(SubscribeServerHandler.java:86)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:84)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:153)
at io.netty.channel.PausableChannelEventExecutor.invokeChannelRead(PausableChannelEventExecutor.java:86)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:389)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:243)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:84)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:153)
at io.netty.channel.PausableChannelEventExecutor.invokeChannelRead(PausableChannelEventExecutor.java:86)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:389)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:956)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:127)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:514)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
一、待查询数据表schemecontenttable如下
二、与数据表对应的实体类SchemeContentTable
@Entity
@BatchSize(size=20)//hibernate的session大小
@Table(name="SchemeContentTable",catalog="anyviewdb")
public class SchemeContentTable implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String vpName;//新的题目名,VID+目录+题目名唯一
private String vchapName;//虚拟目录名
private Float score;//分值
private Timestamp startTime;//允许开始时间
private Timestamp finishTime;//要求完成时间
private Timestamp updateTime;
private SchemeTable scheme;//作业表
private ProblemTable problem;//题目 级联
private int status;//状态
@Column(name="Status",length=11)
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Id
@Column(name="ID",unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="VPName",length=255)
public String getVpName() {
return vpName;
}
public void setVpName(String vpName) {
this.vpName = vpName;
}
@Column(name="VChapName",length=255)
public String getVchapName() {
return vchapName;
}
public void setVchapName(String vchapName) {
this.vchapName = vchapName;
}
@Column(name="Score")
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
@Column(name="StartTime")
public Timestamp getStartTime() {
return startTime;
}
public void setStartTime(Timestamp startTime) {
this.startTime = startTime;
}
@Column(name="FinishTime")
public Timestamp getFinishTime() {
return finishTime;
}
public void setFinishTime(Timestamp finishTime) {
this.finishTime = finishTime;
}
@Column(name="UpdateTime")
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
@ManyToOne(targetEntity=SchemeTable.class)
@JoinColumn(name="VID")
public SchemeTable getScheme() {
return scheme;
}
public void setScheme(SchemeTable scheme) {
this.scheme = scheme;
}
@ManyToOne(targetEntity=ProblemTable.class)
@JoinColumn(name="PID")
public ProblemTable getProblem() {
return problem;
}
public void setProblem(ProblemTable problem) {
this.problem = problem;
}
}
三、封装查询信息的实体类类CatalogInfo
public class CatalogInfo implements Serializable {
private static final long serialVersionUID = 1L;
// 作业内容ID
private Integer pid;
// 虚拟目录
private String chapName;
// 题目名
private String pName;
// 分值
private Float score;
// 开始时间
private Timestamp startTime;
// 结束时间
private Timestamp finishTime;
// 更新时间
private Timestamp updateTime;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getChapName() {
return chapName;
}
public void setChapName(String chapName) {
this.chapName = chapName;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public Timestamp getStartTime() {
return startTime;
}
public void setStartTime(Timestamp startTime) {
this.startTime = startTime;
}
public Timestamp getFinishTime() {
return finishTime;
}
public void setFinishTime(Timestamp finishTime) {
this.finishTime = finishTime;
}
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
public CatalogInfo() {
}
public CatalogInfo(Integer pid, String chapName, String pName, Float score, Timestamp startTime,
Timestamp finishTime, Timestamp updateTime) {
this.pid = pid;
this.chapName = chapName;
this.pName = pName;
this.score = score;
this.startTime = startTime;
this.finishTime = finishTime;
this.updateTime = updateTime;
}
@Override
public String toString() {
return "CatalogInfo [pid=" + pid + ", chapName=" + chapName + ", pName=" + pName + ", score=" + score
+ ", startTime=" + startTime + ", finishTime=" + finishTime + ", updateTime=" + updateTime + "]";
}
}
四、主函数
public void main(String cid){
String query2 = "select new com.anyview.codec.pojo.response.CatalogInfo(t.problem.pid,t.vchapName,t.vpName,t.score,t.startTime,t.finishTime,t.updateTime) from SchemeContentTable as t where t.scheme.vid=? and t.status=1";
List<CatalogInfo> catalogInfoList = getSession().createQuery(query2).setInteger(0, 1).list();
}
五、运行结果
运行失败,抛出异常
六、分析原因
仔细检查之后发现构造器参数个数没有问题,问题肯定是因为CatalogInfo的构造器参数类型和mysql数据库的类型不对应,经过尝试发现,将CatalogInfo的TimeStamp换成Date就可以了
七、修改代码
public class CatalogInfo implements Serializable {
private static final long serialVersionUID = 1L;
// 作业内容ID
private Integer pid;
// 虚拟目录
private String chapName;
// 题目名
private String pName;
// 分值
private Float score;
// 开始时间
private Date startTime;
// 结束时间
private Date finishTime;
// 更新时间
private Date updateTime;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getChapName() {
return chapName;
}
public void setChapName(String chapName) {
this.chapName = chapName;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getFinishTime() {
return finishTime;
}
public void setFinishTime(Date finishTime) {
this.finishTime = finishTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public CatalogInfo() {
}
public CatalogInfo(Integer pid, String chapName, String pName, Float score, Date startTime, Date finishTime,
Date updateTime) {
this.pid = pid;
this.chapName = chapName;
this.pName = pName;
this.score = score;
this.startTime = startTime;
this.finishTime = finishTime;
this.updateTime = updateTime;
}
@Override
public String toString() {
return "CatalogInfo [pid=" + pid + ", chapName=" + chapName + ", pName=" + pName + ", score=" + score
+ ", startTime=" + startTime + ", finishTime=" + finishTime + ", updateTime=" + updateTime + "]";
}
}
结论:mysql数据库中的datime类型对应java中的Date类型