欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ids for this class must be manually assigned before calling save() 异常

程序员文章站 2022-04-12 20:59:52
...
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
	at org.hibernate.id.Assigned.generate(Assigned.java:52)
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
	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:715)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
	at com.ada.tech.sdj.common.persistence.SimpleHibernateDao.save(SimpleHibernateDao.java:89)
	at com.ada.tech.sdj.common.service.BaseService.save(BaseService.java:38)
	at com.ada.tech.sdj.ams.service.sms.CustomSmsRecordService.send(CustomSmsRecordService.java:54)
	at com.ada.tech.sdj.ams.service.sms.CustomSmsRecordService$$FastClassBySpringCGLIB$$7be44301.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 com.ada.tech.sdj.system.utils.MethodTimeAdvice.invoke(MethodTimeAdvice.java:24)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
	at com.ada.tech.sdj.ams.service.sms.CustomSmsRecordService$$EnhancerBySpringCGLIB$$cd601b7e.send(<generated>)
	at com.ada.tech.sdj.ams.service.sms.CustomSmsRecordService$$FastClassBySpringCGLIB$$7be44301.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 com.ada.tech.sdj.system.utils.MethodTimeAdvice.invoke(MethodTimeAdvice.java:24)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	

    解决方案(一):

            若使用注解形式的hibernate则在实体类中添加:@GeneratedValue(strategy = GenerationType.AUTO)

        @Id
	@Column(name="csr_id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long csrId;

          或者:@GeneratedValue(strategy=GenerationType.IDENTITY)

        @Id
	@Column(name="csr_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long csrId;


    解决方案(二):

            若使用xml形式的hibernate则在hibernate.cfg.xml文件中修改配置为:

            <generator class="increment" />

    <id name="id" type="integer">
          <column name="id" />
          <generator class="increment" />
    </id>

(尊重劳动成果,转载请注明出处:http://blog.csdn.net/ud_world/article/details/79312791 ud_world的博客)
相关标签: hibernate 异常