我做得一个struts+hibernate的项目,有一个问题是这样的:student(学生持久化类),team(班级持久化类),academy(学院持久化类),student和team是一对多关系,team和Academy是一对多关系,student和Academy没有直接的关系。现在我想做一个动态查询,其中一段代码是这样的。
下面的代码很多,但是粗略看一下就行,不用一句一句的看
/**
- 根据条件查询 */ public List byStudent(String sno,String sname,String classid,int academyid){
Criteria critera = getSession().createCriteria(Student.class);//里面是一个class对象,而不是HQL
if(sno!=null&&!sno.equals("")){//如果equals在前面的话,容易报 nullpoint 异常
critera.add(Restrictions.like("sno", sno,MatchMode.ANYWHERE));
//如果是进行模糊查询的话,就要加一个参数,MatchMode.ANYWHERE ,否则就不能进行模糊查询
}
if(sname!=null&&!sname.equals("")){
critera.add(Restrictions.like("sname", sname,MatchMode.ANYWHERE));
}
if(address!=null&&!address.equals("")){
critera.add(Restrictions.like("address", address,MatchMode.ANYWHERE));
}
if(classid!=0){
critera.add(Restrictions.eq("classes.cid",new Integer(classid)));
}
if(academyid!=0){
critera.add(Restrictions.like("academyid",new Integer(academyid));
}
return critera.list();
}
问题是这样的,那个academyid也是需要进行动态查询的,但是critera.add(Restrictions.eq("?",new Integer(academyid)));这里的问好我不知道该填写什么,请大家帮一下
[b]问题补充:[/b]
别人贴出的代码都是截图,具体是怎样实现的?
[b]问题补充:[/b]
[code="java"]public class Student implements java.io.Serializable {
private String id; //hibernate主键id 学号
private String name; //学生姓名
private String psw; //登录密码
private Team team; //所在班级
private Grade grade; //所在年级
private Approve approve; //学生是否通过素质拓展认证
private ColumnOne columnOne;
[/code]
[b]问题补充:[/b]
[code="java"]public class Team implements java.io.Serializable {
private String id;
private String name;
private Set<Student> students = new HashSet<Student>();
private Academy academy;[/code]
[b]问题补充:[/b]
[code="java"]public class Academy implements java.io.Serializable {
private String id;
private String name;
private Set<Admin> admins = new HashSet<Admin>();
private Set<Team> teams = new HashSet<Team>();[/code]
[b]问题补充:[/b]
上面的代码参数可能和下面的有一定的不一样,大家尝试着看吧
[b]问题补充:[/b]
谢了,有点急。帮忙看一下
[b]问题补充:[/b]
[code="java"]下面的代码很多,但是粗略看一下就行,不用一句一句的看
/**
- 根据条件查询 */ public List byStudent(String sno,String sname,String classid,int academyid){
Criteria critera = getSession().createCriteria(Student.class);//里面是一个class对象,而不是HQL
if(sno!=null&&!sno.equals("")){//如果equals在前面的话,容易报 nullpoint 异常
critera.add(Restrictions.like("sno", sno,MatchMode.ANYWHERE));
//如果是进行模糊查询的话,就要加一个参数,MatchMode.ANYWHERE ,否则就不能进行模糊查询
}
if(sname!=null&&!sname.equals("")){
critera.add(Restrictions.like("sname", sname,MatchMode.ANYWHERE));
}
if(address!=null&&!address.equals("")){
critera.add(Restrictions.like("address", address,MatchMode.ANYWHERE));
}
if(classid!=0){
critera.add(Restrictions.eq("classes.sid",new Integer(classid)));
}
if(academyid!=0){
critera.add(Restrictions.like("academyid",new Integer(academyid));
}
return critera.list();
}
[/code]
[b]问题补充:[/b]
全部的积分都压上了,帮一下吧
[b]问题补充:[/b]
[code="java"]Criteria critera = getSession().createCriteria(Student.class);
if(classid!=0){
Criteria classes = critera.createCriteria("team");
classes.add(Expression.eq("id", classid));
}
return critera.list();[/code]
你这是什么意思啊,没看明白,classes并没有和critera 关联上。能再详细一点么,加上点注释。
[b]问题补充:[/b]
to yourgame 你的方法我尝试了,不行啊。还有别的办法没?谢了
[code="java"]critera.add(Restrictions.eq("team.academy.id",new Integer(academyid)));
[/code]
[b]问题补充:[/b]
[code="java"]javax.servlet.ServletException: org.hibernate.QueryException: could not resolve property: team.academy.id of: pojo.Student
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
utils.EncodingFilter.doFilter(EncodingFilter.java:25)
root cause
org.hibernate.QueryException: could not resolve property: team.academy.id of: pojo.Student
org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:63)
org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:433)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:393)
org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:82)
org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:67)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
dao.StudentDaoImpl.search(StudentDaoImpl.java:112)
struts.action.StudentAction.search(StudentAction.java:93)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
utils.EncodingFilter.doFilter(EncodingFilter.java:25)
[/code]
就是这个错误,我试过了,只要是属性(team.academy.i)与student中的属性不定应就会有这种错误,
[b]问题补充:[/b]
这是StudentDaoImpl的代码,你看一看
[code="java"] public List search(String id,String name,String academyid,String teamid){
List students = null;
Session sess = HibernateSessionFactory.getSession();
Transaction tran = sess.beginTransaction();
Criteria critera = sess.createCriteria(Student.class);
if(!"".equals(id)&&id!=null){
critera.add(Restrictions.eq("id",id));
}
if(!"".equals(name)&&name!=null){
critera.add(Restrictions.eq("name",name));
}
if(!"".equals(teamid)&&teamid!=null){
critera.add(Restrictions.eq("team.id",teamid));
}
if(!"".equals(academyid)&&academyid!=null){
critera.add(Restrictions.eq("team.academy.id",academyid));
}
students = critera.list();
tran.commit();
sess.close();
return students;
}[/code]