Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Several issues with "adding custom behaviour to all repositories" in spring data jpa

  1. #1
    Join Date
    Nov 2007
    Posts
    177

    Default Several issues with "adding custom behaviour to all repositories" in spring data jpa

    Hello,

    I use Spring data jpa and I am trying to add custom behaviour to all repositories as described here:
    http://static.springsource.org/sprin...l-repositories

    I encountered several issues:

    -First, there is no such method as getDomainClass in the RepositoryMetadata class as described in the Spring documentation (see below):
    Code:
    protected Object getTargetRepository(RepositoryMetadata metadata) {
          return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainClass(), entityManager);
    }
    I used the following method instead: getDomainType()

    Is this right?


    -Second my application throws exceptions when tomcat starts. Here is the full stack trace:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalRepositoryImpl' defined in file [E:\users\jumartin\dev_sts\.metadata\.plugins\org.eclipse.wst.server.core\
    tmp0\wtpwebapps\SuiviTRC\WEB-INF\classes\trc\suivi\repository\GlobalRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could
     not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<i
    nit>()
    Here is my custom global repository code:

    Code:
    public class GlobalRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GlobalRepository<T, ID> {
    
    	private EntityManager em;
    
    	public GlobalRepositoryImpl(Class<T> domainClass, EntityManager em) {
    		super(domainClass, em);
    		this.em = em;
    	}
    
    	public void sharedCustomMethod(ID id) {
    		
    	}
    
    }
    Here is my xml config:

    Code:
    <repositories base-package="trc.suivi.repository" factory-class="trc.suivi.repository.GlobalRepositoryFactoryBean">
    		<repository id="pliRepository" />
    		<repository id="globalRepository" />
    	</repositories>
    I was not able to find any other sample on the web. Can anyone please help?

    Regards,

    J.

  2. #2

    Default

    Quote Originally Posted by balteo View Post
    I used the following method instead: getDomainType()

    Is this right?
    That is the correct method.

    Quote Originally Posted by balteo View Post
    Here is my xml config:

    Code:
    <repositories base-package="trc.suivi.repository" factory-class="trc.suivi.repository.GlobalRepositoryFactoryBean">
    		<repository id="pliRepository" />
    		<repository id="globalRepository" />
    	</repositories>
    This configuration does not match the one given in the reference documentation. Spring Data JPA will find your repositories automatically as long as the base-package is correct. Try changing your XML configuration to:

    Code:
    <repositories base-package="trc.suivi.repository" factory-class="trc.suivi.repository.GlobalRepositoryFactoryBean"/>

  3. #3
    Join Date
    Nov 2007
    Posts
    177

    Default

    Hello Loke,

    Thanks for taking the time to reply.

    I altered the XML config as you advised to this:

    Code:
    	<repositories base-package="trc.suivi.repository" factory-class="trc.suivi.repository.GlobalRepositoryFactoryBean">
    	</repositories>
    Here is what I get:
    Code:
    GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalRepositoryImpl' defined in file [E:\users\jumartin\dev_sts\.metadata\.plugins\org.eclipse.wst.server.core\
    tmp0\wtpwebapps\SuiviTRC\WEB-INF\classes\trc\suivi\repository\GlobalRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could
     not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<i
    nit>()
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
    	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: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.la
    ng.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
    	... 21 more
    Caused by: java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at java.lang.Class.getConstructor0(Class.java:2706)
    	at java.lang.Class.getDeclaredConstructor(Class.java:1985)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:67)
    	... 22 more

  4. #4

    Default

    Hi,

    could you add the source code of the GlobalRepositoryFactoryBean class? Also, are you using Spring Data JPA 1.0.X or 1.1?

  5. #5
    Join Date
    Nov 2007
    Posts
    177

    Default

    Here is the code for GlobalRepositoryFactoryBean:
    Code:
    package trc.suivi.repository;
    
    import java.io.Serializable;
    
    import javax.persistence.EntityManager;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
    import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
    import org.springframework.data.repository.core.RepositoryMetadata;
    import org.springframework.data.repository.core.support.RepositoryFactorySupport;
    
    public class GlobalRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {
    
    	protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
    		return new GlobalRepositoryFactory(entityManager);
    	}
    
    	private static class GlobalRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
    
    		private EntityManager entityManager;
    
    		public GlobalRepositoryFactory(EntityManager entityManager) {
    			super(entityManager);
    			this.entityManager = entityManager;
    		}
    
    		protected Object getTargetRepository(RepositoryMetadata metadata) {
    			return new GlobalRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
    		}
    
    		protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
    			return GlobalRepository.class;
    		}
    	}
    }
    I use: Spring data jpa: 1.1.0 & Spring data common: 1.3.0

    Regards,

    J.

  6. #6

    Default

    I found one problem from your repository factory bean. The method getRepositoryBaseClass returns the type of your base repository interface. It should return the type of your base repository implementation. Try changing this method to:

    Code:
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
    	return GlobalRepositoryImpl.class;
    }
    Let me know if this did the trick.

  7. #7
    Join Date
    Nov 2007
    Posts
    177

    Default

    No luck...

    I changed to GlobalRepositoryImpl.class but it still gives me the following exception:

    Code:
    2012-07-20 13:24:55,125 [Thread-2] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalRepositoryImpl' defined in file [E:\users\jumartin\dev_sts\.metadata\.plugins\org.eclipse.wst.server.core\
    tmp0\wtpwebapps\SuiviTRC\WEB-INF\classes\trc\suivi\repository\GlobalRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could
     not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<i
    nit>()
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
    	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: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.la
    ng.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
    	... 21 more
    Caused by: java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at java.lang.Class.getConstructor0(Class.java:2706)
    	at java.lang.Class.getDeclaredConstructor(Class.java:1985)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:67)
    	... 22 more

  8. #8
    Join Date
    Nov 2007
    Posts
    177

    Default

    I forgot to include the code for GlobalRepositoryImpl:

    Here it is:
    Code:
    package trc.suivi.repository;
    
    import java.io.Serializable;
    
    import javax.persistence.EntityManager;
    
    import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
    
    public class GlobalRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GlobalRepository<T, ID> {
    
    	private EntityManager em;
    
    	public GlobalRepositoryImpl(Class<T> domainClass, EntityManager em) {
    		super(domainClass, em);
    		this.em = em;
    	}
    
    	public void sharedCustomMethod(ID id) {
    		
    	}
    
    }

  9. #9

    Default

    I am running out of ideas here. The only difference between your code and the code that is working for me is that I am using different constructor in my generic base repository. My constructor looks following:

    Code:
    public GenericBaseRepository(JpaEntityInformation<T, ID> entityMetadata, EntityManager entityManager) {
            super(entityMetadata, entityManager);
    }
    Here is the getTargetRepository() method of my repository factory bean.

    Code:
    @Override
    protected Object getTargetRepository(RepositoryMetadata metadata) {
                return new GenericBaseRepository<T, I>((JpaEntityInformation<T,I>) getEntityInformation(metadata.getDomainType()), entityManager);
    }
    If does this does not help, you could add your application context configuration here. It might have some other clues about the problem.

  10. #10
    Join Date
    Nov 2007
    Posts
    177

    Default

    Here is my the changed code:

    Code:
    	public GlobalRepositoryImpl(JpaEntityInformation<T, ID> entityMetadata, EntityManager entityManager) {
    		super(entityMetadata, entityManager);
    	}
    Code:
    	@Override
    		protected Object getTargetRepository(RepositoryMetadata metadata) {
    		            return new GlobalRepositoryImpl<T, I>((JpaEntityInformation<T,I>) getEntityInformation(metadata.getDomainType()), entityManager);
    		}
    and the resulting exception:

    Code:
    GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalRepositoryImpl' defined in file [E:\users\jumartin\dev_sts\.metadata\.plugins\org.eclipse.wst.server.core\
    tmp0\wtpwebapps\SuiviTRC\WEB-INF\classes\trc\suivi\repository\GlobalRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could
     not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<i
    nit>()
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
    	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: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [trc.suivi.repository.GlobalRepositoryImpl]: No default constructor found; nested exception is java.la
    ng.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
    	... 21 more
    Caused by: java.lang.NoSuchMethodException: trc.suivi.repository.GlobalRepositoryImpl.<init>()
    	at java.lang.Class.getConstructor0(Class.java:2706)
    	at java.lang.Class.getDeclaredConstructor(Class.java:1985)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:67)
    	... 22 more

Tags for this Thread

Posting Permissions

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