Spring jpa Entity不是托管类型

时间:2017-09-29 10:38:53

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

我从其他问题尝试了几个解决方案,但没有一个解决了这个问题。

Typos应该不是问题,因为我的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());
    }
}

春天因以下错误而崩溃:

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')
}

2 个答案:

答案 0 :(得分:0)

显然我只需要重新索引所有库,因为它找不到javax.persistence.Entity并使用了hibernate库。

答案 1 :(得分:0)

我在Config类上看不到@SpringBootApplication。

如果您有其他类使用main方法和@SpringBootApplication。

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

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