每次插入记录前需要先删除表的记录,要保证每次插入的序列都是从1开始。所以需要手动删除Sequence再建,
然而在JPA2.0中手动创建Sequence应该怎么写执行语句?或者获取JDBC Connection怎么写? String dropSeq="drop SEQUENCE SEQ_TMP_CRM_OWNER";
java.sql.Connection conn = this.getEntityManager().unwrap(java.sql.Connection.class);
String createSeq="create sequence SEQ_TMP_CRM_OWNER minvalue 1 maxvalue 999999999999999999999999999 start with 1 increment by 1 cache 20";
PreparedStatement stmt=conn.prepareStatement(dropSeq);
stmt.addBatch(createSeq);
stmt.executeBatch();
stmt.close();
conn.close();这样写会抛出异常
javax.persistence.PersistenceException: Hibernate cannot unwrap interface java.sql.Connection
at org.hibernate.ejb.AbstractEntityManagerImpl.unwrap(AbstractEntityManagerImpl.java:986)
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.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy40.unwrap(Unknown Source)
at com.trinasolar.set.crm.dao.impl.CRMOwnerDaoJpaImpl.deleteAll(CRMOwnerDaoJpaImpl.java:90)
at com.trinasolar.set.crm.service.impl.CRMOwnerServiceImpl.getOwnerFromCRM(CRMOwnerServiceImpl.java:69)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy54.getOwnerFromCRM(Unknown Source)
然而在JPA2.0中手动创建Sequence应该怎么写执行语句?或者获取JDBC Connection怎么写? String dropSeq="drop SEQUENCE SEQ_TMP_CRM_OWNER";
java.sql.Connection conn = this.getEntityManager().unwrap(java.sql.Connection.class);
String createSeq="create sequence SEQ_TMP_CRM_OWNER minvalue 1 maxvalue 999999999999999999999999999 start with 1 increment by 1 cache 20";
PreparedStatement stmt=conn.prepareStatement(dropSeq);
stmt.addBatch(createSeq);
stmt.executeBatch();
stmt.close();
conn.close();这样写会抛出异常
javax.persistence.PersistenceException: Hibernate cannot unwrap interface java.sql.Connection
at org.hibernate.ejb.AbstractEntityManagerImpl.unwrap(AbstractEntityManagerImpl.java:986)
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.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy40.unwrap(Unknown Source)
at com.trinasolar.set.crm.dao.impl.CRMOwnerDaoJpaImpl.deleteAll(CRMOwnerDaoJpaImpl.java:90)
at com.trinasolar.set.crm.service.impl.CRMOwnerServiceImpl.getOwnerFromCRM(CRMOwnerServiceImpl.java:69)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy54.getOwnerFromCRM(Unknown Source)
解决方案 »
- Socket送信的outputstream.write()的问题
- 用线程分布去查询数据库。求解!
- 有高手知道银行数据转换系统都用到什么技术!
- struts 500错误问题
- xfire调用webservice接口报错
- 客户自定义产品数据类型的解决方案
- org.hibernate.SessionException: Session is closed异常 求解
- 跪求hibernate synchronizer插件
- J2EE到底什么才属于你的范畴(小弟郁闷呀! 大家来了解一下。。。期待大家来发表意见, 在线等待。。。)
- 急问!关于J2EE项目的测试!100分在线等!
- linux路径问题
- struts2 action传值
JPA 的EntityManager可以强制转换成HibernateEntityManager,得到Session,
再得到Connection public <T> T unwrap(Class<T> clazz) {
if ( Session.class.isAssignableFrom( clazz ) ) {
return ( T ) getSession();
}
if ( SessionImplementor.class.isAssignableFrom( clazz ) ) {
return ( T ) getSession();
}
if ( EntityManager.class.isAssignableFrom( clazz ) ) {
return ( T ) this;
}
throw new PersistenceException( "Hibernate cannot unwrap " + clazz );
}
//参数"aa"是persistence.xml文件中<persistence-unit name="itcast">name的属性值。
EntityManager em = factory.createEntityManager();
em好像可以获取connection,不记得了,lz试试吧