Is there any one has an example?
Entitymanager would manage the transaction in Hibernate EntityManager.
Is there any way to use Spring to manage it?
Is there any one has an example?
Entitymanager would manage the transaction in Hibernate EntityManager.
Is there any way to use Spring to manage it?
There will be. We have some prototype code and as soon as the spec is final we will provide this support. I hope we will have something in the sandbox before that - I'll remind Juergen that we need this now![]()
Is there any timelines?
The code is in the CVS and the nightly builds already. It's in the tiger directory.
I'm playing around w/ it and it works quite well! I have just one problem regarding integration tests. I try to verify my JPA code with direct JDBC (via jdbcTemplate). I provide the connection details in my persistence.xml (no JNDI or JTA). Without defing a dataSource bean in my app ctx, jdbcTemplate can't be initialized. Defining a BasicDataSource makes the jdbcTemplate work, but IMO not in the same session/transaction with JPA. The persisted (and flushed but uncommited) JPA-changes are not visible via jdbcTemplate queries (in the same test method). The same approach works for my "classic" hibernate tests.
Any ideas? Something I'm doing wrong?
Sebastian
Any ideas? trisberg? Juergen?
Hi Sebastian,
Are you using Hibernate Entity Manager as your JPA implementation? If so, would you mind sharing your code? I'm just getting started with Spring's JPA support and would love to see what you've managed so far.
Thanks,
Jim
Your "classic" hibernate tests were probably sharing the same db resource (conenction) and could have been sharing the same transaction because of a non-JTA setup.
The JPA stuff may be using JTA transactions that scope the EntityManager's context to the JTA transaction. "Breaking in" to this transaction with another connection would not be possible.
You may be able to set your underlying transaction isolation level for all database connections to "READ_UNCOMMITED". This should allow your JDBC connection to observe the uncommitted results of the JPA-based transaction.
In the meantime, I stumbled across some slides and sample code that Thomas Risberg made available through the Philadelphia Spring Users Group. Thanks Thomas.
http://www.springdeveloper.com/psug/meeting.html
Hi Jim.
I also used Thomas' slides to get started
I know about the transaction issue regarding my tests. I solved the problem by retrieving the current connection from the EntityManager:
This works for me because I'm not using Spring's (JUnit-based) base test classes anymore: I rolled my own ones (I use TestNG now).Code:protected Connection getJdbcConnection() { return ((EntityManagerImpl) EntityManagerFactoryUtils.getEntityManager((EntityManagerFactory) applicationContext.getBean("entityManagerFactory"))).getSession().connection(); }
A better solution would be to extend DefaultJpaDialect and override its "getJdbcConnection" method w/ the something like this:
This new HibernateJpaDialect (or whatever) has to be injected into your JpaTransactionManager (in your app ctx) - together w/ a matching data source.Code:public ConnectionHandle getJdbcConnection(final EntityManager em, boolean readOnly) throws PersistenceException, SQLException { return new SimpleConnectionHandle((((EntityManagerImpl) em).getSession().connection()); }
This last part is untested, but according to the docs, it should workAnd make Spring's base test classes and JdbcTemplate work!
Sebastian