hibernate实现ID序列自增

注解方式开发中
实体类里的id 配合数据库的序列 , 实现id自增
第一步: 数据库里边要新建一个序列,最好以相关表中的id命名.
程序中的实体类例代码如下:

@Entity 
@Table(name = "XXGX_RBBTJ")
public class StatisticalDaily {
	/** id */
	@Id
	@Column(name = "ID", unique = true, nullable = false)
	@SequenceGenerator(name = "RBBTJ_SEQ", sequenceName = "RBBTJ_SEQ", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RBBTJ_SEQ")
	private Integer id;
	/** 查询日期 */
	@Column(name = "QUERY_TIME")
	private String queryTime;
	/** 申请笔数 */
	@Column(name = "APPLY_COUNT")
	private Integer applyCount;
	/** 成功笔数(总的 O) O=B+C+D */
	@Column(name = "SUC_TOTAL_COUNT")
	private Integer sucTotalCount;
  1. @SequenceGenerator(name = “RBBTJ_SEQ”, sequenceName = “RBBTJ_SEQ”, allocationSize = 1) 主要是把数据库的序列绑定上,且记只完成这一步是不行的. 仅仅注入了序列
  2. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “RBBTJ_SEQ”) 该步骤是指定id生成策略,并且指定了使用哪个序列.

一下就是我没有加上id生成策略所报的错误信息:
javax.persistence.PersistenceException:ids for this class must be manually assigned before calling save(): shgjj.xxgx.channels.statistics.bo.StatisticalDaily

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): shgjj.xxgx.channels.statistics.bo.StatisticalDaily
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:898)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:294)
at com.sun.proxy.$Proxy50.merge(Unknown Source)
at gov.util.jpa.impl.BaseJpaDaoImpl.save(BaseJpaDaoImpl.java:125)
at gov.util.jpa.impl.BaseJpaDaoImpl.save(BaseJpaDaoImpl.java:106)
at gov.util.jpa.impl.BaseJpaDaoImpl$$FastClassBySpringCGLIB$$6bfb1cb7.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:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
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 shgjj.xxgx.channels.statistics.dao.impl.StatisticDaoImpl$$EnhancerBySpringCGLIB$$ff869e07.save(<generated>)
at shgjj.xxgx.channels.statistics.service.impl.StatisticServiceImpl.statisticDaliy(StatisticServiceImpl.java:80)
at shgjj.xxgx.channels.statistics.service.impl.StatisticServiceImpl$$FastClassBySpringCGLIB$$b532a34c.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:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
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 shgjj.xxgx.channels.statistics.service.impl.StatisticServiceImpl$$EnhancerBySpringCGLIB$$ed358dd9.statisticDaliy(<generated>)
at shgjjtest.wlpt.service.minzheng.Dsr.test(Dsr.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.ParentR

猜你喜欢

转载自blog.csdn.net/fanbaodan/article/details/85168686