Spring Data: 未管理的类型:class java.lang.Object

12

我正在尝试通过遵循不同的Spring教程,通过Spring Boot公开我的数据库表格,并且我遇到了以下异常(我将在最后发布异常)。

这是我正在使用的类:

package com.xxx.xxx.db;

import org.springframework.data.repository.Repository;

import java.io.Serializable;

public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID>{
  T findOne(ID id);
  T save(T entity);
}

这是我的具体服务类

package com.xxx.xxx.tablename;

import com.xxx.xxx.db.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TableNameService {

  @Autowired
  private BaseRepository<TableName, Long> repository;

  public TableName findById(Long id){
    return repository.findOne(id);
  }
}

这是我的实体

package com.xxx.xxx.tablename;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@Entity
@Table(name="table_name")
public class TableName {

  @Id
  private long id;

  @NotNull
  private String name;

  public TableName() {
  }

  public TableName(long id){
    this.id = id;
  }

  public TableName(String name){
    this.name = name;
  }

  public TableName(long id, String name){
    this.id = id;
    this.name = name;
  }
}

我的Spring配置是通过`application.yml`文件完成的。该文件目前只包含数据库连接信息,除此之外没有其他信息。
我还有一个名为`BeansConfig`的类,它被注释为`@Configuration`。但这个类被用来验证由`application.yml`提供的数据库属性是否有效。
现在出现了异常。
springframework.beans.factory.BeanCreationException: Error creating bean with name 'TableNameService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.xxx.db.BaseRepository com.xxx.xxx.tablename.TableNameService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    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:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at com.xxx.xxx.Application.main(Application.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.xxx.db.BaseRepository com.xxx.xxx.tablename.TableNameService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 17 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': 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:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    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:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 19 common frames omitted
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.getEntityInformation(JpaEntityInformationSupport.java:67)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 29 common frames omitted

你可以使用Spring Data已经提供的存储库接口,例如JpaRepository。尝试为你的Table类创建一个特定的接口,类似于"TableRepository extends JpaRepository<Table, Long>",并用@Repository注释进行标注。 - lenach87
1
如果您在尝试实现仓库层次结构时遇到此错误,请在自定义父仓库接口上放置@NoRepositoryBean注释。 - chill appreciator
2个回答

17

你现在的尝试方式是不可行的。因为T需要是一个Entity,所以你需要对它进行类型限制。一种选择是为每个具体实体创建一个仓库,但这里有一种更通用的实现方式:

使用一个基类作为你的实体:

@Entity
public abstract class BaseClass<IdType extends Serializable>{

    private IdType id;

    //Getter and setters

}

为您的基类定义一个@Repository

@Repository
public interface Dao<T extends BaseClass, IdType extends Serializable> extends
        CrudRepository<T, IdType> {

}

你可以在这个接口中定义共享的实体操作,并在需要更具体的情况下从中继承。

另请参见:


这是我很想实现的东西,但现在我正在努力为特定类编写一个仓库。我添加了一个类TableNameGatewayRepo<TableName,ID extends Serializable> extends JpaRepoistory<PaymentGateway,ID>,但我仍然得到相同的错误。 - Em Ae
尝试使用 TableNameGatewayRepo extends CrudRepository<PaymentGateway, Long> 或者用 Jpa 替换 Crud 来实现相同的功能。 - Aritz

0
这是一个旧帖子,但我遇到了同样的问题,所以让我分享一下当前的解决方案。
有一个新的注解@NoRepositoryBean来处理这种情况。它可以添加在扩展Repository接口上,但它指示上下文不将其初始化为实际的Spring Data JPA Repository。

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/NoRepositoryBean.html

你只需要使用这个注解来注释你的自定义仓库,然后再为实际的JPA仓库进行扩展。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接