I am using hibernate 3 and JPA configured via Springs entity manager factory (see snippet from application-context.xml below).
Unfortunately in one part of my app i need to contruct queries that jpa cannot handle, i would like to get hold of a java.sql.Connection as i think that with this object i will be able to execute the queries that i need.
I realize that this is not the desired way to work with jpa but see no other alternatives.
I am having problems retrieving the connection object.
I have tried
andCode:Connection con = ((org.hibernate.ejb.EntityManagerImpl) em).getSession().connection();
But neither will let me cast the entity manager em to the implementation object even though when i debug the code the entity manager has the class org.hibernate.ejb.EntityManagerImpl. The error that they give isCode:Connection con = ((org.hibernate.ejb.AbstractEntityManagerImpl) em).getSession().connection();
This is the first time that i've worked with entity managers and JPA and i may be missing something obvious. Perhaps someone can point me in the right direction.Code:$Proxy38 cannot be cast to org.hibernate.ejb.EntityManagerImpl
Heres the snippet that corresponds to my entity manager factory configuration in application-context.xml. The entity manager is correctly injected into the class where i need to get the Connection.
Any help would be greatly appreciatedCode:<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> </bean> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/mydatabase" /> <property name="username" value="XXX" /> <property name="password" value="XXX" /> </bean>
max


Reply With Quote