1. 程式人生 > >hibernate實現ID序列自增

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