java - Spring JpaRepository using generic entity -
i'm trying implement generic dao use x entities quite similar (id, code, description):
@repository public interface genericdao<t> extends jparepository<t, long> { t findbycode(string code); t findbyid(long id); }
one of entities this:
@getter @setter @entity @table(name = "test") public class test {...}
i've got service need use dao:
@service public class testserviceimpl implements testservice { private genericdao<test> testdao; @autowired public testserviceimpl(genericdao<test> testdao) { this.testdao = testdao; } }
when start application using springboot application doesn't start , error is:
caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'genericdao': invocation of init method failed; nested exception java.lang.illegalargumentexception: not managed type: class java.lang.object @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1574) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:539) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.findautowirecandidates(defaultlistablebeanfactory.java:1127) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1044) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:942) @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:813) @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ... 23 more caused by: java.lang.illegalargumentexception: not managed type: class java.lang.object @ org.hibernate.jpa.internal.metamodel.metamodelimpl.managedtype(metamodelimpl.java:219) @ org.springframework.data.jpa.repository.support.jpametamodelentityinformation.<init>(jpametamodelentityinformation.java:68) @ org.springframework.data.jpa.repository.support.jpaentityinformationsupport.getmetadata(jpaentityinformationsupport.java:67) @ org.springframework.data.jpa.repository.support.jparepositoryfactory.getentityinformation(jparepositoryfactory.java:145) @ org.springframework.data.jpa.repository.support.jparepositoryfactory.gettargetrepository(jparepositoryfactory.java:89) @ org.springframework.data.jpa.repository.support.jparepositoryfactory.gettargetrepository(jparepositoryfactory.java:69) @ org.springframework.data.repository.core.support.repositoryfactorysupport.getrepository(repositoryfactorysupport.java:172) @ org.springframework.data.repository.core.support.repositoryfactorybeansupport.initandreturn(repositoryfactorybeansupport.java:239) @ org.springframework.data.repository.core.support.repositoryfactorybeansupport.afterpropertiesset(repositoryfactorybeansupport.java:225) @ org.springframework.data.jpa.repository.support.jparepositoryfactorybean.afterpropertiesset(jparepositoryfactorybean.java:92) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1633) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1570) ... 34 more
you can't this. spring trying create repository on start, not on autowiring moment. spring absolutely has no idea, kind of generic param should use. trying instantiate repository object
. object
not entity, corresponding error.
conclusion: repository must of specific type, not generic.
Comments
Post a Comment