// 评价表
public class ApproveInfo {
/**
* @hibernate.id generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private Date approveTime;
/**
* 评论者
* @hibernate.many-to-one
*/
private User approver;
/**
* @hibernate.property
*/
private String comment;
/**
* @hibernate.many-to-one
*/
private Document document;}
// 文章表
public class Document { public static final String STATUS_NEW = "新建";
public static final String STATUS_FINISH = "结束"; /**
* @hibernate.id generator-class="native"
*/
private int id; /**
* 论文标题
*
* @hibernate.property
*/
private String title; /**
* 论文名称
*
* @hibernate.property
*/
private String content;
/**
* 关键字
*
* @hibernate.property
*/
private String keyword; /**
* 英文标题
*
* @hibernate.property
*/
private String englishTitle; /**
* 英文摘要
*
* @hibernate.property
*/
private String englishSummary; /**
* 英文关键字
*
* @hibernate.property
*/
private String englishKeyword; /**
* 专业
*
* @hibernate.property
*/
private String major; /**
* 删除标识 默认为0:可用,1不可用
*
* @hibernate.property
*/
private Integer deleteFlag;
/**
* 论文附件
*
* @hibernate.property
*/
private String otherContent; /**
* 论文完成时间
*
* @hibernate.property
*/
private Date finishTime;
/**
* 论文提义交时间
* @hibernate.property
*/
private Date createTime; /**
* 多端执有一端的一个实体映射
* @hibernate.many-to-one
*/
private User creator; /**
* 论文状态
* @hibernate.property
*/
private String status;
}现在要写一条sql 语句,当用登陆者可以看到我评价过的所有文章,条件是往年的也有现在的!
// 从评论表中查询
public PagerModel findDocumentListByUserId(int teacherId, int year) {
String hql = "select a.document from ApproveInfo a,Document d on a.document.id = d.id where a.approver.userId=?";
if (year != 0) {
hql += " and YEAR(d.finishTime) = " + year;
}
return searchPaginated(hql, new Object[] {teacherId,year});
}以上是我写的hql但会报错!Stacktraces
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 82 [select count(*) from com.eomist.model.ApproveInfo a,com.eomist.model.Document d on a.document.id = d.id where a.approver.userId=? and YEAR(d.finishTime) = 2010]
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
public class ApproveInfo {
/**
* @hibernate.id generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private Date approveTime;
/**
* 评论者
* @hibernate.many-to-one
*/
private User approver;
/**
* @hibernate.property
*/
private String comment;
/**
* @hibernate.many-to-one
*/
private Document document;}
// 文章表
public class Document { public static final String STATUS_NEW = "新建";
public static final String STATUS_FINISH = "结束"; /**
* @hibernate.id generator-class="native"
*/
private int id; /**
* 论文标题
*
* @hibernate.property
*/
private String title; /**
* 论文名称
*
* @hibernate.property
*/
private String content;
/**
* 关键字
*
* @hibernate.property
*/
private String keyword; /**
* 英文标题
*
* @hibernate.property
*/
private String englishTitle; /**
* 英文摘要
*
* @hibernate.property
*/
private String englishSummary; /**
* 英文关键字
*
* @hibernate.property
*/
private String englishKeyword; /**
* 专业
*
* @hibernate.property
*/
private String major; /**
* 删除标识 默认为0:可用,1不可用
*
* @hibernate.property
*/
private Integer deleteFlag;
/**
* 论文附件
*
* @hibernate.property
*/
private String otherContent; /**
* 论文完成时间
*
* @hibernate.property
*/
private Date finishTime;
/**
* 论文提义交时间
* @hibernate.property
*/
private Date createTime; /**
* 多端执有一端的一个实体映射
* @hibernate.many-to-one
*/
private User creator; /**
* 论文状态
* @hibernate.property
*/
private String status;
}现在要写一条sql 语句,当用登陆者可以看到我评价过的所有文章,条件是往年的也有现在的!
// 从评论表中查询
public PagerModel findDocumentListByUserId(int teacherId, int year) {
String hql = "select a.document from ApproveInfo a,Document d on a.document.id = d.id where a.approver.userId=?";
if (year != 0) {
hql += " and YEAR(d.finishTime) = " + year;
}
return searchPaginated(hql, new Object[] {teacherId,year});
}以上是我写的hql但会报错!Stacktraces
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 82 [select count(*) from com.eomist.model.ApproveInfo a,com.eomist.model.Document d on a.document.id = d.id where a.approver.userId=? and YEAR(d.finishTime) = 2010]
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
public PagerModel findDocumentListByUserId(int teacherId, int year) {
String hql = "select a.document from ApproveInfo a,Document d where a.approver.userId = ?";
if (year != 0) {
hql += " and YEAR(d.finishTime) = " + year;
}
return searchPaginated(hql, new Object[] {teacherId,year});
}结果:
java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
if(teacherId!=0){
hql += "and a.approver.userId = ?" + teacherId;
}
if(year!=0){
hql += " and YEAR(d.finishTime) = " + year;
}
String hql = "select a.document from ApproveInfo a,Document d on a.document.id = d.id where a.approver.userId=?";
on是多余的
我改成这样的可以了!
//String hql = "select a.document from ApproveInfo a, Document d where a.document.id=d.id and a.approver.userId = ? and YEAR(d.finishTime) = ?";但是在导出数据时!
//Label name = new Label(4, j+1, data.getCreator().getLoginName()!=null?data.getCreator().getLoginName():"");
Label name = new Label(4, j+1, data.getCreator().getPerson().getName()!=null?data.getCreator().getPerson().getName():"");取不到值了!