【问题标题】:Spring jpa Entity is not a managed typeSpring jpa Entity 不是托管类型
【发布时间】:2018-03-11 06:08:31
【问题描述】:

我已经尝试了其他问题的几个解决方案,但都没有解决问题。

错字不应该是问题,因为我的 ide 正确突出了包名称。

这是我的设置:

实体:

package sh.owl.hootament.backend.database.entities;

@Entity
@Table(name = "Creators")
public class CreatorEntity {    
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    private String id;

    @Column
    private String username;

    @Column
    private String email;

    // Getters and settters
    [...]
}

存储库:

package sh.owl.hootament.backend.database.repositories;

@EnableAutoConfiguration
@RepositoryRestResource(path = "creators")
public interface CreatorEntityRepository extends JpaRepository<CreatorEntity, String> {
}

配置类:

@Configuration
@EnableSpringConfigured
@EnableJpaRepositories(basePackages = "sh.owl.hootament.backend.database.repositories")
@EntityScan(basePackages = "sh.owl.hootament.backend.database.entities")
@ComponentScan("sh.owl.hootament.*")
public class SpringConfiguration {
}

要测试的索引控制器:

@Controller
public class IndexController {
    @Autowired
    private CreatorEntityRepository creatorEntityRepository;

    @GetMapping("/")
    public String index(Model model) {
        creatorEntityRepository.saveAndFlush(new CreatorEntity());
    }
}

Spring 崩溃并出现以下错误:

Caused by: java.lang.IllegalArgumentException: Not a managed type: class sh.owl.hootament.backend.database.entities.CreatorEntity
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:153) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:100) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:82) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.7.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.7.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.7.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 34 common frames omitted

编辑,添加 gradle 文件:

buildscript {
ext {
    springBootVersion = '1.5.7.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.data:spring-data-rest-hal-browser')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.session:spring-session')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.3.RELEASE")

compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')

testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}

【问题讨论】:

  • 检查错别字。
  • @Entity.. 包是什么?
  • 包名是:sh.owl.hootament.backend.database.entities;
  • 是 javax.persistence.Entity 吗?
  • 啊!我正在使用休眠@Entity 属性。我编辑了帖子以添加 gradle(通过 start.spring.io 生成)文件。由于javax.persistence.Entity 不存在,我是否缺少库?

标签: spring jpa spring-boot spring-data spring-data-jpa


【解决方案1】:

显然我只需要重新索引所有库,因为它找不到 javax.persistence.Entity 并改用休眠的。

【讨论】:

    【解决方案2】:

    我在 Config 类上看不到 @SpringBootApplication。

    如果你有其他一些带有 main 方法和 @SpringBootApplication 的类。

    尝试在你的主类上移动@EnableJpaRepositories 和@EntityScan。

    否则,如果您的 Config 类是主类,则在其上添加 @SpringBootApplication

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 2015-09-18
      • 2015-04-24
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2019-12-30
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多