Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: How to integrate Spring and Hibernate Entitymanager?

  1. #1
    Join Date
    Jul 2005
    Posts
    24

    Unhappy How to integrate Spring and Hibernate Entitymanager?

    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?

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    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
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Jul 2005
    Posts
    24

    Default

    Is there any timelines?

  4. #4
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    The code is in the CVS and the nightly builds already. It's in the tiger directory.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  5. #5
    Join Date
    Jul 2005
    Location
    Rüdesheim, Germany
    Posts
    29

    Default

    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

  6. #6
    Join Date
    Jul 2005
    Location
    Rüdesheim, Germany
    Posts
    29

    Default

    Any ideas? trisberg? Juergen?

  7. #7
    Join Date
    Aug 2004
    Location
    Columbus, OH
    Posts
    65

    Default Share?

    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

  8. #8
    Join Date
    Aug 2004
    Location
    Columbus, OH
    Posts
    65

    Default

    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.

  9. #9
    Join Date
    Aug 2004
    Location
    Columbus, OH
    Posts
    65

    Default

    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

  10. #10
    Join Date
    Jul 2005
    Location
    Rüdesheim, Germany
    Posts
    29

    Default

    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:

    Code:
        protected Connection getJdbcConnection() {
            return ((EntityManagerImpl) EntityManagerFactoryUtils.getEntityManager((EntityManagerFactory) applicationContext.getBean("entityManagerFactory"))).getSession().connection();
        }
    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).


    A better solution would be to extend DefaultJpaDialect and override its "getJdbcConnection" method w/ the something like this:

    Code:
    	public ConnectionHandle getJdbcConnection(final EntityManager em, boolean readOnly)
    			throws PersistenceException, SQLException {
    
    		return new SimpleConnectionHandle((((EntityManagerImpl) em).getSession().connection());
    	}
    This new HibernateJpaDialect (or whatever) has to be injected into your JpaTransactionManager (in your app ctx) - together w/ a matching data source.

    This last part is untested, but according to the docs, it should work And make Spring's base test classes and JdbcTemplate work!

    Sebastian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •