Execption:the database returned no natively generated identity value

org.hibernate.HibernateException: The database returned no natively generated identity value
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:90)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:100)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at com.bjsxt.dao.impl.UserDAOImpl.save(UserDAOImpl.java:27)
at com.bjsxt.service.UserService.add(UserService.java:33)
at com.bjsxt.service.UserService$$FastClassBySpringCGLIB$$189fc15d.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.bjsxt.service.UserService$$EnhancerBySpringCGLIB$$52fc0e09.add(<generated>)
at com.bjsxt.service.UserServiceTest.testAdd(UserServiceTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

使用技术:spring+hibernate

异常原因:spring整合hibernate后,直接在spring的session-Factory中进行配置,并通过annotation(@entity)设置映射但是没有通过annotation指定主键的增长方式,在实例化对象后没有为id赋初值就直接持久化到数据库,并且在数据库端没有设置id为auto_increment

导致结果:数据库端的主键值无法确定,即为null,这违背了数据库主键的实体完整性

deeeeebug:解决1:添加注释:在getId函数上添加annotation

@GenericGenerator(name="generatsr",strategy="increment") //设置增长方式
                            @GeneratedValue(generator="generatsr")

解决2:在实例持久化对象的时候,为主键赋初值

解决3:在数据库端设置主键为auto_increment

时间: 2024-10-23 21:26:01

Execption:the database returned no natively generated identity value的相关文章

数据库建表时出现的org.hibernate.HibernateException: The database returned no natively generated identity value的错误

错误原因: 我设置的Id的类型是:自动增长型(如下),但是我在数据库中设置的Id确是:int类型.所以类型不匹配.导致返回: org.hibernate.HibernateException:数据库返回的没有本地生成的标识值的错误. @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } 改正方法: 在cmd中操作数据库输入: ALTER TABLE tea

Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your datab

Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed? 这样的问题一般出如今时差配置的问题上 首先错误信息提示是否定义你数据库的时区,是否安装pytz 网络上有说是由于mysql时区的问题,可是改动了貌似作用不大 最后发现是project中settings.py中的配置问题,修正 USE_TZ = Fals

Django Ubuntu:Database returned an invalid value in QuerySet.dates(). 错误的解决方法

运行Windows下创建的Django项目时,发生此错误! Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed? 按提示先安装pytz 1.先安装easy_install: sudo apt-get install python-setuptools 2.进入官网,获取下载地址,使用wget命令下载: wget https://pypi.pytho

Django(博客系统):按照时间分层筛选“/blog/article/?create_time__year=2017”,出现问题:Database returned an invalid datetime value. Are time zone definitions for your database installed?

问题背景 添加文章时间没问题,但为了设定博客文章按照时间分层筛选(创建时间的年份.年月&月份来搜索文章),我在blog这个django app的admin.py的ArticleAdmin类中做了如下设置: date_hierarchy = 'create_time' # 详细时间分层筛选 models.Article中create_time定义如下: create_time = models.DateTimeField(u'创建时间', auto_now_add=True) 设置后,后台呈现效果

解决Database returned an invalid datetime value. Are time zone definitions for your database installed?

设定博客文章按照时间分层筛选出现问题 ret=Article.objects.filter(user=user).annotate(month=TruncMonth("create_time")).values("month").annotate(c=Count("nid")).values_list("month","c") print("ret----->",ret) 解决方案

报错Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?解决

在django中的setting.py中: LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Shanghai' #'UTC' USE_I18N = True USE_L10N = True USE_TZ = True 由于USE_TZ=True开启了导致错误 解决方法: 在终端输入以下代码 mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql 输入mysql密码即可(注意上面语句中的m

Database returned an invalid value in QuerySet.datetimes()

安装pytz模块 修改setting.py,添加 import pytz from pytz import * 修改mysql timezone 执行如下命令:mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p  mysql 重启数据库

mysql 定义自增

The database returned no natively generated identity value问题 alter table user_table MODIFY user_id INT UNSIGNED AUTO_INCREMENT;

Hibernate 常见异常

Hibernate 常见异常net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误. 当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中. 2