我在JPA中使用以下代码,以便在使用本机查询插入后返回自动生成的ID:
Query q = em.createNativeQuery("insert into .... returning ID", Long.class); q.executeUpdate();
但是,出现以下错误:
A result was returned when none was expected
好的,那很容易。我刚刚使用了q.getSingleResults(),效果很好!
Query q = em.createNativeQuery(sql); BigInteger biid = (BigInteger) q.getSingleResult(); long id = biid.longValue();