return getHibernateTemplate().find("from EmpworkInfo a where a.projectInfo.proId=? and a.user.username = ? and a.workDate>=? and a.workDate<=? order by a.workDate desc", new Object[] {byreport.getProId(),byreport.getUsername(),byreport.getStartTime(),byreport.getEndTime()});我的是这样写的,后面用一个Object 数组传参!!
就是对于mysql翻译过来是错的 试试6楼的方法先吧
HQL本来就不需要写select 的 代码看看也没看出什么问题 要么用 StringBuffer str = new StringBuffer ();str.append(" from Admin a where a.username='"); str.append(username); str.append("' and a.password ='"); str.append(password); str.append("'"); 试试看吧
private static void addStudentIntoTeam(String tname){ String hql = "from Student s where s.team is null"; String hqlTwo = "from Team t where t.tname=?"; Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); Query qTwo = session.createQuery(hqlTwo); qTwo.setString(0,tname); Team team = (Team)qTwo.uniqueResult(); Query q = session.createQuery(hql); List students = q.list(); for(Iterator it=students.iterator();it.hasNext();){ Student student = (Student)it.next(); System.out.println(student.getId()); team.getStudents().add(student); session.update(team); } tx.commit(); session.close(); } 我的数据库也是mysql,设置参数的方法也是差不多的。只不过我用的是setString()。没报错误呀
str = " from Admin a where a.username = ? and a.password = ?"; 个人感觉问题现在在小写的a上。你把它改成这样; str = " from Admin where Admin.username = ? and Admin.password = ?";
q.setParameter(1, password);
q.setString(0,username);这样就可以了.
q.setString(0,username);和q.setString(1,password);吧?个人看法
2 q.setParameter(0, username);
3 q.setParameter(1, password);
注意。参数还没加进去呢。就执行。顺序好像不对吧。
new Object[] {byreport.getProId(),byreport.getUsername(),byreport.getStartTime(),byreport.getEndTime()});我的是这样写的,后面用一个Object 数组传参!!
要么用
StringBuffer str = new StringBuffer ();str.append(" from Admin a where a.username='");
str.append(username);
str.append("' and a.password ='");
str.append(password);
str.append("'");
试试看吧
String hql = "from Student s where s.team is null";
String hqlTwo = "from Team t where t.tname=?";
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Query qTwo = session.createQuery(hqlTwo);
qTwo.setString(0,tname);
Team team = (Team)qTwo.uniqueResult();
Query q = session.createQuery(hql);
List students = q.list();
for(Iterator it=students.iterator();it.hasNext();){
Student student = (Student)it.next();
System.out.println(student.getId());
team.getStudents().add(student);
session.update(team);
}
tx.commit();
session.close();
}
我的数据库也是mysql,设置参数的方法也是差不多的。只不过我用的是setString()。没报错误呀
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="myeclipse.connection.profile">bug</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/bug
</property>
<property name="connection.username">root</property>
<property name="connection.password">6331616</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="bug/entity/Admin.hbm.xml" />
<mapping resource="bug/entity/Buglist.hbm.xml" /></session-factory></hibernate-configuration>
个人感觉问题现在在小写的a上。你把它改成这样;
str = " from Admin where Admin.username = ? and Admin.password = ?";