如何使用Hibernate获取最后插入的id

时间:2022-09-25 15:34:49

I want to fetch the last inserted value's id in Hibernate.

我想在Hibernate中获取最后插入的值的id。

After search:

搜索后:

Long lastId = ((Long) session.createSQLQuery("SELECT LAST_INSERT_ID()").uniqueResult()).longValue();

But the following code gives me this error:

但是下面的代码给了我这个错误:

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long

Please share your thoughts!

请分享你的想法!

Solution

Long lastId = ((BigInteger) session.createSQLQuery("SELECT LAST_INSERT_ID()").uniqueResult()).longValue();

Don't forget to import:

别忘了导入:

import java.math.BigInteger;

import java.math.BigInteger;

4 个解决方案

#1


10  

Error is pretty clear. It's returning BigInteger and not long

错误很清楚。它正在返回BigInteger而不是很久

You have to assign it to a BigInteger. And get longValue() from it.

您必须将其分配给BigInteger。并从中获取longValue()。

#2


4  

public Integer save(Smartphones i) {
    int id = 0;
    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction trans=session.beginTransaction();
    try{
        session.save(i);   
        id=i.getId();
        trans.commit();     
    }
    catch(HibernateException he){}
    return id;
}

#3


2  

You can simply use save which returns a Serializable object that actually is the last insert id. Sample code:

您可以简单地使用save,它返回一个实际上是最后一个插入ID的Serializable对象。示例代码:

Session session=this.getSessionFactory().getCurrentSession();
    int result = -1;
    try {
        Serializable ser = session.save(paper);
        if (ser != null) {
            result = (Integer) ser;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;

A testing run:

测试运行:

int result = paperService.save(paper);
    System.out.println("The id of the paper you just added is: "+result);

and here is the output:

这是输出:

The id of the paper you just added is: 3032

#4


0  

Since the return type of uniqueResult() is BigInteger and not Long, you should do it like this:

由于uniqueResult()的返回类型是BigInteger而不是Long,所以你应该这样做:

long lastId = session.createSQLQuery("SELECT LAST_INSERT_ID()")
                     .uniqueResult()  // this returns a BigInteger
                     .longValue();    // this will convert it to a long value

The method uniqueResult() only returns a BigInteger because of your query SELECT LAST_INSERT_ID().

由于您的查询SELECT LAST_INSERT_ID(),uniqueResult()方法仅返回BigInteger。

#1


10  

Error is pretty clear. It's returning BigInteger and not long

错误很清楚。它正在返回BigInteger而不是很久

You have to assign it to a BigInteger. And get longValue() from it.

您必须将其分配给BigInteger。并从中获取longValue()。

#2


4  

public Integer save(Smartphones i) {
    int id = 0;
    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction trans=session.beginTransaction();
    try{
        session.save(i);   
        id=i.getId();
        trans.commit();     
    }
    catch(HibernateException he){}
    return id;
}

#3


2  

You can simply use save which returns a Serializable object that actually is the last insert id. Sample code:

您可以简单地使用save,它返回一个实际上是最后一个插入ID的Serializable对象。示例代码:

Session session=this.getSessionFactory().getCurrentSession();
    int result = -1;
    try {
        Serializable ser = session.save(paper);
        if (ser != null) {
            result = (Integer) ser;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;

A testing run:

测试运行:

int result = paperService.save(paper);
    System.out.println("The id of the paper you just added is: "+result);

and here is the output:

这是输出:

The id of the paper you just added is: 3032

#4


0  

Since the return type of uniqueResult() is BigInteger and not Long, you should do it like this:

由于uniqueResult()的返回类型是BigInteger而不是Long,所以你应该这样做:

long lastId = session.createSQLQuery("SELECT LAST_INSERT_ID()")
                     .uniqueResult()  // this returns a BigInteger
                     .longValue();    // this will convert it to a long value

The method uniqueResult() only returns a BigInteger because of your query SELECT LAST_INSERT_ID().

由于您的查询SELECT LAST_INSERT_ID(),uniqueResult()方法仅返回BigInteger。