Vish
Vish

Reputation: 346

How to write GenericRepository in spring jpa

I want retrieve report using spring JPA from multiple table. So i'm passing java.lang.Object as entity to JpaRepository. I've tried something like

Case 1:

@NoRepositoryBean
public interface ObjectRepository extends JpaRepository<Object, Long> {

    @Query(value = "SELECT new InOutReport(it.customer_name 'partyName', if(it.in_out_action =0,'SALE','PURCHASE') 'transactionType', it.invoice_no 'invoiceNo', im.product_name 'productName' ,it.qty 'qty',it.handed_over_to_or_by 'handedOverToOrBy', it.date 'date')"
            + " FROM inventory_transaction it, inventory_master im WHERE it.inventory_id = im.inventory_id")
    List<InOutReport> getInOutReport();
}

I will get error like

APPLICATION FAILED TO START
***************************

Description:

Field objectRepository in com.test.service.ReportServiceImpl required a bean of type 'com.test.repository.ObjectRepository' that could not be found.


Action:

Consider defining a bean of type 'com.test.repository.ObjectRepository' in your configuration.

Case 2: If i define @Repository annotation

@Repository
public interface ObjectRepository{
    ...
}

I'll get error like

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportServiceImpl': Unsatisfied dependency expressed through field 'objectRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 37 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:153)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:100)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:82)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 47 common frames omitted

Please suggest where am i doing wrong.

Upvotes: 0

Views: 282

Answers (2)

Sangam Belose
Sangam Belose

Reputation: 4506

You can create a db view from those multiple table and then create entity for the same. This should solve your issue if you are using it for reporting purpose only. but you wont be able to update anything on the same.

@Entity
@Table(name="VW_reports")
@NamedQuery(name="ViewReports.findAll", query="SELECT v FROM ViewReports v")
public class ViewReports {
}

Upvotes: 0

davidxxx
davidxxx

Reputation: 131526

This error means that Object is not considered an entity:

Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object

And it is the case : Object is not an entity.
If you want to handle all your entities with the same Spring repository, you should introduce a base class and makes all report classes extend this base class.

Then use this base class to parameter your repository :

public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {

Of course, it makes sense if all your report classes have a common structure. Otherwise, you will have to do some downcasts and you will finish with a brittle and not readable code.

Upvotes: 1

Related Questions