I have some data in my database, and there is an id column which increments automatically whenever a data is stored.
我的数据库中有一些数据,并且有一个id列,每当存储数据时,它都会自动递增。
For some purpose I want to retrieve the last row added or the row with max id (which is also the last row). I am writing the below query as:
出于某种目的,我希望检索添加的最后一行或具有max id的行(也是最后一行)。我将下面的查询写成:
List<Student> student = sessionFactory.getCurrentSession().createQuery("SELECT * FROM Student ORDER BY id DESC")
I don't know whether it is right way to do, but while executing I am getting error like unexpected token: *.
我不知道这是不是正确的做法,但在执行过程中,我得到了意外的错误,比如:*。
Looking for your valuable suggestion and thanks in advance.
期待您的宝贵建议和感谢。
public void storeData(Student student)
{
List<Student> std = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC limit 1").list(); //getting error while executing this statement(unexpected token limit)
System.out.println(std.get(0).getNumber()); //getting error while executing it
}
I am sending the student data through student object in storeData class. I want that after storing data I can access the value of data stored, like I want to store the number value in a variable for further process. But while executing the above I am getting error.
我正在通过storeData类中的student对象发送学生数据。我希望在存储数据之后,能够访问存储的数据的值,就像我希望将数字值存储在一个变量中,以便进行进一步的处理一样。但是在执行上述操作时,我得到了错误。
4 个解决方案
#1
3
Why not simply the "highest" ID?
为什么不简单地“最高”ID ?
select max(id) from student
Using the session factory:
使用会话工厂:
sessionFactory.getCurrentSession().createQuery("select max(id) from student");
#2
1
Set Max Result option .setMaxResults(1)
设置Max结果选项。setmaxresults (1)
There are two ways
有两种方法
1) with List as output using .setMaxResults(1).list()
1)使用.setMaxResults(1).list()将List作为输出
So Your statement will become and you need to iterate it
所以你的语句会变成,你需要迭代它
List <Student> student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC").setMaxResults(1).list();
2) As you only need last result then you can also go for .setMaxResults(1).uniqueResult()
2)因为只需要最后一个结果,所以也可以使用。setmaxresults (1).uniqueResult()
uniqueResult() : Convenience method to return a single instance that matches the query, or null if the query returns no results.
So Your statement will become
所以你的陈述会变成
Student student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC").setMaxResults(1).uniqueResult();
Using any method you can save your id(max) in int variable
使用任何方法都可以在int变量中保存id(max)。
[Note]: Remove Limit 1 from query
[注]:从查询中删除Limit 1
Hope it will help you
希望它能帮到你。
#3
0
SELECT * FROM Student ORDER BY id DESC limit 1
#4
0
Remove SELECT *
from your query, HQL doesn't need it:
从查询中删除SELECT *, HQL不需要:
List<Student> student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC");
#1
3
Why not simply the "highest" ID?
为什么不简单地“最高”ID ?
select max(id) from student
Using the session factory:
使用会话工厂:
sessionFactory.getCurrentSession().createQuery("select max(id) from student");
#2
1
Set Max Result option .setMaxResults(1)
设置Max结果选项。setmaxresults (1)
There are two ways
有两种方法
1) with List as output using .setMaxResults(1).list()
1)使用.setMaxResults(1).list()将List作为输出
So Your statement will become and you need to iterate it
所以你的语句会变成,你需要迭代它
List <Student> student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC").setMaxResults(1).list();
2) As you only need last result then you can also go for .setMaxResults(1).uniqueResult()
2)因为只需要最后一个结果,所以也可以使用。setmaxresults (1).uniqueResult()
uniqueResult() : Convenience method to return a single instance that matches the query, or null if the query returns no results.
So Your statement will become
所以你的陈述会变成
Student student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC").setMaxResults(1).uniqueResult();
Using any method you can save your id(max) in int variable
使用任何方法都可以在int变量中保存id(max)。
[Note]: Remove Limit 1 from query
[注]:从查询中删除Limit 1
Hope it will help you
希望它能帮到你。
#3
0
SELECT * FROM Student ORDER BY id DESC limit 1
#4
0
Remove SELECT *
from your query, HQL doesn't need it:
从查询中删除SELECT *, HQL不需要:
List<Student> student = sessionFactory.getCurrentSession().createQuery("FROM Student ORDER BY id DESC");