Unable to get Session Object from Jpa 2 Entitymanager when my app.ear deployed in jboss 7.1.1
johnibosco Jul 26, 2013 8:27 AMHi,
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) {
session.persist(t);
if ( ++count % 5000 == 0 ) {
session.flush();
session.clear();
}
}
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) {
session.persist(t);
if ( ++count % 5000 == 0 ) {
session.flush();
session.clear();
}
}
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="datasource" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.ctx.myentity</class>
<!-- <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> -->
<properties>
<!-- 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" />
</properties>
</persistence-unit>
</persistence>
and my jboss-deployment-structure.xml
================================
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<!-- Make sub deployments isolated by default, so they cannot see each others classes without a Class-Path entry -->
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.javassist" />
<module name="org.apache.log4j" />
<module name="org.hibernate"></module>
<module name="org.slf4j" />
</exclusions>
<dependencies>
<module name="org.hornetq" export="true" />
<module name="deployment.myproject.ear" export="true" />
</dependencies>
</deployment>
<sub-deployment name="mywebproject.war">
</sub-deployment>
</jboss-deployment-structure>
Please tell me how to resolve this issue.I have tried many times but not able to fix this bug.
Regards
Bosco