spring data jpa:不是托管类型:xxx类

Posted

技术标签:

【中文标题】spring data jpa:不是托管类型:xxx类【英文标题】:spring data jpa: Not an managed type: class xxx 【发布时间】:2015-09-18 08:40:59 【问题描述】:

我遇到了 spring data jpa 的问题。我已经克隆了这个存储库https://github.com/royclarkson/spring-rest-service-oauth,它工作得很好。然后我想用真实的数据库替换 import.sql ,我想把它和 postgres 连接起来。 我跟着下一个教程:http://devcrumb.com/hibernate/spring-data-jpa-hibernate-maven

它识别出我的数据库,但有一些运行时错误:

10:41:00.281 [hello.Application.main()] WARN o.s.c.s.ClassPathXmlApplicationContext - 上下文初始化期间遇到异常 - 取消刷新尝试 org.springframework.beans.factory.BeanCreationException:创建名为“userRepository”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException: Not an managed type: class hello.model.Role 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 在 hello.Application.main(Application.java:29) [classes/:na] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79] 在 java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79] 在 org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418) [spring-boot-maven-plugin-1.2.2.RELEASE.jar:1.2.2.RELEASE] 在 java.lang.Thread.run(Thread.java:745) [na:1.7.0_79]

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jpaData" />
</persistence>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:context="http://www.springframework.org/schema/context"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">

<!-- Directory to scan for repository classes -->
<jpa:repositories base-package="hello.data" />

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>org.postgresql.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:postgresql://localhost:5432/locoDB</value>
    </property>
    <property name="username">
        <value>postgres</value>
    </property>
    <property name="password">
        <value>postgres</value>
    </property>
</bean>

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="jpaData" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

</beans>

这是整个项目:https://github.com/criticalbh/spring-oauth-postgres

【问题讨论】:

不能直接用 DB 替换 SQL 文件,还需要配置。请发布您的配置。 请编辑您的主帖,并在那里添加整个配置。删除评论。 【参考方案1】:

最后我得到了

@Configuration
@EnableJpaRepositories(basePackages = 
    "hello.data"
)
@EnableTransactionManagement
@EnableAutoConfiguration
public class PersistenceContext 

@Bean(destroyMethod = "close")
DataSource dataSource(Environment env) 
   // HikariConfig dataSourceConfig = new HikariConfig();
    BoneCPDataSource dataSource = new BoneCPDataSource();
    dataSource.setDriverClass("org.postgresql.Driver");
    dataSource.setJdbcUrl("jdbc:postgresql://localhost:5432/locoDB");
    dataSource.setUsername("postgres");
    dataSource.setPassword("postgres");

    return dataSource;


@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource   dataSource,
                                                            Environment  env) 
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =  new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource);
    entityManagerFactoryBean.setJpaVendorAdapter(new   HibernateJpaVendorAdapter());
    entityManagerFactoryBean.setPackagesToScan("hello.data");

    Properties jpaProperties = new Properties();

    //Configures the used database dialect. This allows Hibernate to  create SQL
    //that is optimized for the used database.
    jpaProperties.put("hibernate.dialect",  "org.hibernate.dialect.PostgreSQL82Dialect");

    //Specifies the action that is invoked to the database when the Hibernate
    //SessionFactory is created or closed.
    jpaProperties.put("hibernate.hbm2ddl.auto",
            "update"
    );

    //Configures the naming strategy that is used when Hibernate creates
    //new database objects and schema elements
    jpaProperties.put("hibernate.ejb.naming_strategy",
           "org.hibernate.cfg.ImprovedNamingStrategy"
    );

    //If the value of this property is true, Hibernate writes all SQL
    //statements to the console.
    jpaProperties.put("hibernate.show_sql",
            "true"
    );

    //If the value of this property is true, Hibernate will format the SQL
    //that is written to the console.
    jpaProperties.put("hibernate.format_sql",
        "true"
    );

    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;


@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) 
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;


应用类: AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(PersistenceContext.class);

【讨论】:

【参考方案2】:

Not a managed type 错误通常表明持久性单元不“知道”您的实体类。

persistence.xml 中,您应该列出您希望 Hibernate 像这样“管理”的实体类:

<persistence-unit name="jpaData">
    <class>your.package.here.YourEntity</class>
</persistence-unit>

【讨论】:

以上是关于spring data jpa:不是托管类型:xxx类的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot JPA 错误:无法处理托管/反向引用“defaultReference”:从类型中找不到反向引用属性

Spring Data Jpa 自定义 repository 转 DTO

处理 JPA 规范和 spring-data-jpa 时如何使用声明 Stream 作为返回类型

spring data jpa:插入而不是删除

即使 JPA 实体不脏,我是不是可以强制 spring-data 更新可审计字段?

Spring Data 系列学习Spring Data JPA 基础查询