7 Replies Latest reply on Aug 27, 2013 8:25 AM by johnibosco

    Unable to get Session Object from Jpa 2 Entitymanager when my app.ear deployed in jboss 7.1.1




      I have migrated my application from jboss 5 to jboss 7.1.1 using spring 3 ,jpa2 and hibernate 4.0.1 as provider and deployed my ear in jboss 7.1.1.It got deployed sucessfully but  when i try to insert bulk data it throws error. It worked with jpa 1.0 and hibernate 3 ,after migrating to jpa2.0 it did not work.


      Actually i need to insert  ~1 million records at a time ,so i used to get Session object from jpa entitty manger and inserted entities in bulk as below.


      Jpa 1.0 (This worked well)


      Session session = (Session) entityManager.getDelegate();

              int count =0;


              for (T t : entities) {



                  if ( ++count % 5000 == 0 ) {









      and now i updated from jpa 1.0 to jpa 2 and hibernate 3 to hibernate 4.0.1 and converted my above code to as blow to make bulk insert.


      jpa 2.0(It did not work)


      Session session = entityManager.unwrap(Session.class);

              int count =0;


              for (T t : entities) {


                  if ( ++count % 5000 == 0 ) {







      But it throws error saying  Hibernate cannot unwrap interface org.hibernate.Session


      17:36:51,109 ERROR [stderr] (jmsContainer-1) javax.persistence.PersistenceException: Hibernate cannot unwrap interface org.hibernate.Session


      17:36:51,125 ERROR [stderr] (jmsContainer-1)     at org.hibernate.ejb.AbstractEntityManagerImpl.unwrap(AbstractEntityManagerImpl.java:1156)


      17:36:51,125 ERROR [stderr] (jmsContainer-1)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


      17:36:51,125 ERROR [stderr] (jmsContainer-1)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


      17:36:51,125 ERROR [stderr] (jmsContainer-1)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


      17:36:51,140 ERROR [stderr] (jmsContainer-1)     at java.lang.reflect.Method.invoke(Method.java:597)


      17:36:51,140 ERROR [stderr] (jmsContainer-1)     at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)


      17:36:51,140 ERROR [stderr] (jmsContainer-1)     at $Proxy40.unwrap(Unknown Source)



      Here is my persistence xml :


      <?xml version="1.0" encoding="UTF-8"?>

      <persistence xmlns="http://java.sun.com/xml/ns/persistence"


          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"


          <persistence-unit name="datasource" >




              <!-- <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> -->


                  <!-- Note: setting 'hibernate.hbm2ddl.auto' to 'create' will result in

                      'import.sql' (in the root of the classpath) being used to populate the DB

                      <property name="hibernate.hbm2ddl.auto" value="create" /> -->

              <property name="hibernate.show_sql" value="true" />

              <property name="hibernate.jdbc.batch_size" value="5000" />



                <property name="hibernate.order_updates" value="true"/>

                <property name="hibernate.order_inserts" value="true"/>

                <property name="hibernate.jdbc.batch_versioned_data" value="true"/>

                <property name="hibernate.jdbc.fetch_size" value="500"/>

                <property name="hibernate.default_batch_fetch_size" value="500"/>

                <property name="hibernate.connection.release_mode" value="auto"/>


                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />








      and my jboss-deployment-structure.xml



      <?xml version="1.0" encoding="UTF-8"?>


        <!-- Make sub deployments isolated by default, so they cannot see each others classes without a Class-Path entry -->





          <!-- Exclusions allow you to prevent the server from automatically adding some dependencies     -->


              <module name="org.javassist" />

              <module name="org.apache.log4j" />

              <module name="org.hibernate"></module>

              <module name="org.slf4j" />





            <module name="org.hornetq" export="true" />

              <module name="deployment.myproject.ear" export="true" />



        <sub-deployment name="mywebproject.war">







      Please tell me how to resolve this issue.I have tried many times but not able to fix this bug.