每次插入记录前需要先删除表的记录,要保证每次插入的序列都是从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)

解决方案 »

  1.   

    其实,你看看EntityManager的源代码就知道了。
    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 );
    }
      

  2.   

    this.getEntityManager().unwrap(java.sql.Connection.class);你这句有问题  EntityManagerFactory factory = Persistence.createEntityManagerFactory("aa");  
            //参数"aa"是persistence.xml文件中<persistence-unit&nbsp;name="itcast">name的属性值。  
            EntityManager em = factory.createEntityManager();  
    em好像可以获取connection,不记得了,lz试试吧