Announcement Announcement Module
Collapse
No announcement yet.
spring-data-jpa + spring-data-mongodb Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data-jpa + spring-data-mongodb

    I am struggling with making these work together. My overall spring framework is version 3.0.7.RELEASE.


    my pom.xml has these specific references:
    Code:
            <dependency>
               <groupId>org.springframework.data</groupId>
               <artifactId>spring-data-mongodb</artifactId>
               <version>1.0.1.RELEASE</version>
           </dependency>
            <dependency>
               <groupId>org.springframework.data</groupId>
               <artifactId>spring-data-jpa</artifactId>
               <version>1.0.3.RELEASE</version>
           </dependency>
    When I add the following line to my applicationContext.xml

    Code:
         <jpa:repositories base-package="mylibrary.repositories"></jpa:repositories>
    I receive this error on server startup (using Tomcat)

    Code:
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:526)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    	at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:410)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:142)
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
    	at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
    	at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:102)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1441)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1410)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
    	... 25 more

    From what I can tell, this is due to lack of a persistence.xml. Despite scouring the internet, I've yet to determine precisely what should be IN the persistence.xml. It is also suggested that this error relates to a lack of an EntityManagerFactory.

    The spring-data-mongodb reference doc seems to suggest that there is no persistence.xml that needs be added, however I suspect this is merely misleading me.

    I am sort of at a loss after struggling with this for far too long, and would appreciate some guidance/thoughts.

    If there need be any more information, please inquire.


    EDIT:

    determined a resolution. Add something similar to the following to your .xml:
    Code:
        <bean class="org.springframework.data.mongodb.core.MongoFactoryBean" id="mongo">
            <property name="host" value="localhost"></property>
        </bean>

    But of course now, when I try to wire up my repository

    Code:
    package mylibrary.repositories;
    
    import java.util.List;
    
    import org.springframework.data.mongodb.repository.Query;
    import org.springframework.data.repository.CrudRepository;
    
    import mylibrary.Entity;
    
    public interface EntityRepository extends CrudRepository<Entity, String> {
    	@Query("{ 'assoc_id' : ?0 }")
    	List<Entity> findByAssocId(String assocId);
    }
    I get an error with a stack trace ending in

    Code:
    Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityManager
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    	... 33 more
    Which I assume means I'm missing a library, but in googling around, cannot determine what that library is.

    Maybe, is anyone aware of an example project similar to what I'm trying to achieve that I could look over and compare to my own?
    Last edited by kgargano; May 25th, 2012, 08:31 AM.

  • #2
    Please use the search as I answered the exact question earlier this week.

    Using spring-data-jpa doesn't free you from providing a JPA entitymanagerfactory this is still needed, I suggest a read of the normal spring framework guide on how to configure a entitymanagerfactory.

    Comment


    • #3
      Thank you for your reply Marten! I actually came across a similar thread and that helped move me forward.

      I now have the configuration set up like

      Code:
      <mongo:mongo id="mongo" host="${mongoHost}" port="${mongoPort}" />
          
          <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      	    <constructor-arg ref="mongo" />
              <constructor-arg value="databaseName" />
          </bean>
          
          <mongo:repositories base-package="mylibrary.repositories" />
      Another important prerequisite is to use mongo-java-driver 2.7.2+

      That said, my project now runs, but

      Code:
      	@Autowired private EntityRepository entityRepository;
      is null at runtime.

      I suspect this is because the entitymanagerfactory is still not properly configured. I'm not sure why the error disappeared, though, if it wasn't resolved...

      EDIT:

      Revising previous suspicion. I don't think my issue continues to relate to entityManagerFactory. Instead, I am just sort of at a loss as to the cause. Currently investigating a means of manually creating the repository rather than relying on Autowired... But that seems hacky. Hopefully I will come across some information. I've mostly been searching for keywords such as mango, mangodb, null, autowired, repository, in any number of combinations. If anyone has suggestions for other keywords to use, even something as simple as that could be helpful. I will report back when I finally discover a resolution, or if we decide to not use the library.


      EDIT2:

      I have made progress. I was able to make autowiring work properly, I neglected to add a declaration to my .xml
      Code:
          <context:component-scan base-package="myLibrary.repositories">
          </context:component-scan>
      Though that makes the repository get autowired, I am unable to query the db (com.mongodb.MongoException: not talking to master and retries used up) which I suspect is due to an error in the way I have configured my DB connection. I will be exploring that, now. Hopefully my experience helps someone else in the future.

      EDIT3 - RESOLUTION:
      Resolved. As hypothesized, the last issue was caused by a configuration misstep. Specifically, the configuration was referencing a secondary DB, but it needed to reference the Master DB.
      Last edited by kgargano; May 25th, 2012, 11:07 AM. Reason: Resolved

      Comment

      Working...
      X