Announcement Announcement Module
Collapse
No announcement yet.
Error when adding Query Methods to repository interface Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error when adding Query Methods to repository interface

    Using Spring Data MongoDB 1.0.0 M4, I am trying to use repositories instead of creating dao's. I am able to create a new repository and the default generated methods work correctly.

    But if I add query methods, I get errors during the server startup.

    I'm at a loss as to how to troubleshoot this. Any thoughts?

    Code:
    ERROR [2011-09-08 14:39:47,552] [Thread-1] (ContextLoader.java:227) - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': FactoryBean threw exception on object creation; nested exception is java.lang.VerifyError: Cannot inherit from final class
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1440)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    	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:4723)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.VerifyError: Cannot inherit from final class
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    	at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2820)
    	at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1150)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    	at org.springframework.data.mongodb.repository.MongoRepositoryFactoryBean$MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactoryBean.java:220)
    	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:303)
    	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:157)
    	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
    	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    	... 18 more
    my application-mongo.xml file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    	xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<mongo:mapping-converter base-package="com.han.model" />
    
    	<mongo:mongo id="replicaSetMongo"
    		replica-set="127.0.0.1:27017,localhost:27018,localhost:27019" />
    	<mongo:db-factory id="mongoDbFactory" dbname="han"
    		mongo-ref="replicaSetMongo" />
    
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg ref="mongoDbFactory" />
    		<constructor-arg name="mongoConverter" ref="mappingConverter" />
    	</bean>
    
    	<mongo:repositories base-package="com.han.repositories" />
    
    	<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
    	<context:annotation-config />
    
    </beans>
    I am using the Person class out of the http://static.springsource.org/sprin...ngo-repo-usage document

    PersonRepository.java
    Code:
    package com.han.repositories;
    
    import java.util.List;
    
    import org.springframework.data.mongodb.repository.MongoRepository;
    
    import com.han.model.Person;
    
    public interface PersonRepository extends
    		MongoRepository<Person, String> {
    
    	// additional custom finder methods go here
    //This line causes this to fail.
    	List<Person> findByLastname(String lastname);
    
    //This line also causes this to fail.
        //Page<Person> findByFirstname(String firstname, Pageable pageable);
    }

  • #2
    Some more info.

    I was reading about custom implementations of repositories, and set one up for a different model class. I'm still having the problem when I add query methods to the repository interface. But I'm able to add methods to the custom interface and implement them.

    For now I am adding the Query Methods to the custom interface, and implementing them manually in the custom impl class. That way, when I figure out why this is breaking, I can just remove the manually created query methods and use the generated ones without changing my client code.

    As a further experiment, I tried to make my custom impl class implement the repository interface, rather than just the custom interface; this required making the custom impl abstract. The goal of this experiment was to be able to use the automatically generated methods, such as count() or my query methods, from within the custom impl class. This failed with the same error as before, "VerifyError: Cannot inherit from final class". So instead I'm autowiring the repository into the custom impl class, even though it's basically a self-reference.

    Comment


    • #3
      So the problem was I was still using spring-data-commons-core 1.1.0.RELEASE, but Spring Data MongoDB 1.0.0 M4 is dependent on spring-data-commons-core 1.2.0.M1. This isn't documented anywhere that I could find.

      Comment


      • #4
        It's documented in the SD MongoDB 1.0.0.M4 pom.xml, actually . Generally the way you ended up with is the intended one. If you need access to methods of the finder repo inside your custom implementation simply wire it into the implementation.

        Comment

        Working...
        X