Spring JpaRepository使用通用实体

时间:2016-01-28 10:58:52

标签: java spring hibernate generics spring-data-jpa

我正在尝试实现这样的通用DAO,以便将它与X实体非常相似(id,代码,描述):

@Repository
public interface GenericDao<T> extends JpaRepository<T, Long> {

    T findByCode(String code);
    T findById(Long id);
}

我的一个实体是这样的:

@Getter
@Setter
@Entity
@Table(name = "TEST")
public class Test {...}

我有一个服务,我需要使用Dao:

@Service
public class TestServiceImpl implements TestService {

    private GenericDao<Test> testDao;

    @Autowired
    public TestServiceImpl(GenericDao<Test> testDao) {
        this.testDao = testDao;
    }
}

当我使用SpringBoot启动应用程序时,应用程序无法启动,错误是:

Caused by: org.springframework.beans.factory.BeanCreationException: Error     creating bean with name 'genericDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 23 more
Caused by: java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:67)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:172)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 34 more

2 个答案:

答案 0 :(得分:4)

你不能这样做。 Spring试图在开始时创建您的存储库,而不是在自动装配时刻。但Spring绝对不知道应该使用什么样的通用参数。所以它试图用Object实例化这个存储库。但Object不是实体,因此您会收到相应的错误。

结论:您的存储库必须是特定类型,但不是通用的。

答案 1 :(得分:0)

您可以通过一些调整来完成您想完成的事情。

与其使用@Repository标记您的GenericDao,不如如此使用它,而应使用@NoRepositoryBean注释。作为JavaDocs的状态,Spring JPA将尝试初始化附加到JpaRepository的任何内容,但是@NoRepositoryBean允许您创建中间扩展,这些扩展不会被初始化,您的真实存储库可以从中扩展。您只需要确保实现GenericDao类的所有对象确实为T提供了一个参数即可。

这意味着您还必须实现GenericDao的扩展,该扩展实际上将具有@Repository批注,并在服务中使用这些dao代替泛型。因此您的代码将如下所示:

通用道:

@NoRepositoryBean
public interface GenericDao<T> extends JpaRepository<T, Long> {

    T findByCode(String code);
    T findById(Long id);
}

TestDao,您将添加的新类是参数化实现:

@Repository
public interface TestDao extends GenericDao<Test> {

}

服务:

@Service
public class TestServiceImpl implements TestService {

    private TestDao testDao;

    @Autowired
    public TestServiceImpl(TestDao testDao) {
        this.testDao = testDao;
    }
}