Announcement Announcement Module
Collapse
No announcement yet.
How to integrate Spring and Hibernate Entitymanager? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      Is there any timelines?

      Comment


      • #4
        The code is in the CVS and the nightly builds already. It's in the tiger directory.

        Comment


        • #5
          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

          Comment


          • #6
            Any ideas? trisberg? Juergen?

            Comment


            • #7
              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

              Comment


              • #8
                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.

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      Guys,

                      What versions of Hibernate, Hibernate Entity Manager and Spring are you using?

                      I've grabbed the sample code from Thomas' presentation, but can't seem to get it to do what I want. Keeping the code as it is, I get:

                      Code:
                      java.lang.NullPointerException
                      	at com.springdeveloper.service.BeerDistributorServiceJpa.getCustomerList(BeerDistributorServiceJpa.java:27)
                      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
                              ...
                      getEntityManager() is null. I can force creation of it by EntityManagerFactory.createEntityManager(), but them I'm faced with

                      Code:
                      javax.persistence.TransactionRequiredException: no transaction is in progress
                      	at org.hibernate.ejb.AbstractEntityManagerImpl.checkTransactionActive(AbstractEntityManagerImpl.java:130)
                      	at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:145)
                      	at com.springdeveloper.service.BeerDistributorServiceJpa.saveCustomer(BeerDistributorServiceJpa.java:48)
                      I'm using the JpaTransactionManager as supplied in the sample code, but it doesn't seem to create a new tx when required.

                      FYI, Hibernate 3.1rc1, Hibernate EM 3.1beta4, Spring 1.3RC1

                      Can anyone shed some light on this?

                      thanks!

                      Comment


                      • #12
                        I've put together a small tutorial describing all this stuff:

                        http://jroller.com/page/sbeigel?entr...va_persistence

                        Sebastian

                        Comment


                        • #13
                          Top work!

                          I was extending JpaSupport (not JpaDaoSupport) and using getEntityManager() rather than using the JpaTemplate.

                          All works now.. thanks for your help

                          Comment


                          • #14
                            Is the JPA preview code still available? I noticed that it was removed from CVS by robharrop on 12/7/05 with the comment "Removed old JPA code". Has this code simply been moved elsewhere and if so where?

                            Comment


                            • #15
                              It's moved to the sandbox -- and currently not included in the 2.0 milestone builds. Don't know why actually...

                              Sebastian

                              Comment

                              Working...
                              X