无法在Spring项目中将java.math.biginger转换为java.util.List
我有密码:无法在Spring项目中将java.math.biginger转换为java.util.List,java,spring,hibernate,hql,Java,Spring,Hibernate,Hql,我有密码: public Student LoginStudent(Student student) { List<Student> students = new ArrayList<Student>(); sessionFactory.getCurrentSession().getTransaction().begin(); String hql = "select stu_id,name from student where usern
public Student LoginStudent(Student student) {
List<Student> students = new ArrayList<Student>();
sessionFactory.getCurrentSession().getTransaction().begin();
String hql = "select stu_id,name from student where username = "ap@gmail.com";
students = (List<Student>) sessionFactory.getCurrentSession().createSQLQuery(hql).uniqueResult();
if (students.size() > 0) {
return students.get(0);
} else {
return null;
}
}
我搜索了谷歌,某处显示
return ((BigInteger)LoginStudent.get(0)).longValue();
但是我必须如何使用它呢?将hql更改为
String hql = "from Student where username = 'ap@gmail.com'";
students = (List<Student>) sessionFactory.getCurrentSession().createQuery(hql).list();
String hql=“from Student where username=”ap@gmail.com'";
students=(List)sessionFactory.getCurrentSession().createQuery(hql.List();
javadoc表示
方法返回与查询匹配的单个实例,如果查询不返回结果,则返回null
但是您需要一个
列表
来检索,因此使用List()
请将行从
students = (List<Student>) sessionFactory.getCurrentSession().createSQLQuery(hql).uniqueResult();
students=(List)sessionFactory.getCurrentSession().createSQLQuery(hql.uniqueResult();
到
students=(List)(sessionFactory.getCurrentSession().createSQLQuery(hql.uniqueResult());
试一试,如果uniqueResult()返回一个student对象列表,它应该可以工作。要使hql正常工作,您需要执行以下操作:
Student student = null;
sessionFactory.getCurrentSession().getTransaction().begin();
String hql = "select stu_id,name from student where username = :username";
Object[] result = sessionFactory.getCurrentSession()
.createSQLQuery(hql)
.setParameter("username", "ap@gmail.com")
.uniqueResult();
if(result != null) {
// manually convert your selection into a Student
student = new Student();
s.setId((Long) result[0]);
s.setName((String) result[1]);
}
return student;
select new Student(s.stu_id,s.name) from Student s where...
必须这样做的原因是,您在hql查询中从Student中选择了某些字段。请参见或声明一个构造函数,以便您的查询如下所示:
Student student = null;
sessionFactory.getCurrentSession().getTransaction().begin();
String hql = "select stu_id,name from student where username = :username";
Object[] result = sessionFactory.getCurrentSession()
.createSQLQuery(hql)
.setParameter("username", "ap@gmail.com")
.uniqueResult();
if(result != null) {
// manually convert your selection into a Student
student = new Student();
s.setId((Long) result[0]);
s.setName((String) result[1]);
}
return student;
select new Student(s.stu_id,s.name) from Student s where...
应该可以这样做。我使用了list()…我得到错误:--java.math.biginger不能强制转换为com.student.pack.Studentreturn students.get(0)-----此行
Student
是一个列表
,您返回它的第一个元素,即Student
。这里没有biginger
,除非您手动强制转换它。不要将不是BigInteger
实例的对象强制转换到此类型。并且在代码中使用正确的命名,如果您编写hql
,那么就使用hql;如果您想使用SQL,那么就编写SQL
。我将添加行来执行HQL,而不是SQL,如果您仍然想使用SQL,则读取该行。@Salini:听起来您没有使用Roman对HQL的更改,请先尝试一下。当您运行SQL“选择stu_id,name from student where username=”ap@gmail.com“
您得到的结果是什么?