2015-07-01 61 views
1

我遇到了spring數據jpa的問題。我已經克隆了這個存儲庫https://github.com/royclarkson/spring-rest-service-oauth,它工作得很好。然後我想用真正的數據庫替換import.sql,並且我想用postgres連接它。 我跟着一個教程:http://devcrumb.com/hibernate/spring-data-jpa-hibernate-mavenspring數據jpa:不是託管類型:類xxx

它認識我的數據庫,但有一些運行時錯誤:

 
    10:41:00.281 [hello.Application.main()] WARN  o.s.c.s.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class hello.model.Role 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) [spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at hello.Application.main(Application.java:29) [classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79] 
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79] 
    at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418) [spring-boot-maven-plugin-1.2.2.RELEASE.jar:1.2.2.RELEASE] 
    at 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

+0

你不能直接用數據庫替換SQL文件,你也必須配置它。請發佈您的配置。 –

+0

請編輯你的主帖,並在那裏添加整個配置。刪除評論。 –

回答

3

Not a managed type錯誤通常表明持久性單元不知道您的實體類。

persistence.xml你應該列出你想Hibernate把「管理」這樣的實體類:

<persistence-unit name="jpaData"> 
    <class>your.package.here.YourEntity</class> 
</persistence-unit> 
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);

相關問題