2017-02-18 101 views
0

我正在尋找一個適用於hibernate 4和mysql的spring引導應用程序的工作版本。我被卡住了spring.config,persistence.xml和pom文件的不同組合

我收到以下錯誤與我的配置,因爲昨天:(:

org.springframework.beans.factory.BeanCreationException:錯誤 創建名稱爲豆「的entityManagerFactory」在類路徑中定義 資源[春天-config.xml中]:init方法的調用失敗;嵌套 例外是java.lang.IllegalStateException:JPA PersistenceProvider類 返回null的EntityManagerFactory - 檢查你的JPA提供者設置

這是我persistence.xml文件

<?xml version="1.0" encoding="UTF-8" ?> 


<persistence 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_2_0.xsd" 
     version="2.0"> 
<persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.PersistenceProvider</provider> 

    <class>com.crossover.trial.travelmanagementportal.model.Order</class> 
    <class>com.crossover.trial.travelmanagementportal.model.User</class> 
    <properties> 

    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> 
    <property name="hibernate.connection.username" value="root"/> 
    <property name="hibernate.connection.password" value="12345678"/> 
    <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/travelmanagement" /> 
    <property name="hibernate.default_schema" value="travelmanagement" /> 
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
    <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
    <property name="hibernate.show_sql" value="true"/> 

    </properties> 
    </persistence-unit> 
</persistence> 

這是我spring.config文件

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/tx"> 


    <context:annotation-config /> 

    <context:property-placeholder location="classpath:application.properties" /> 

    <context:component-scan base-package="com.crossover.trial.travelmanagementportal" /> 


    <!-- For @Transactional annotations --> 

    <!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: --> 
    <bean 
     class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <!-- Drives transactions using local JPA APIs --> 

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

    <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
     <property name="showSql" value="true"/> 
     <property name="generateDdl" value="true"/> 
     <property name="database" value="MYSQL"/> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="my-pu" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter" ref="jpaAdapter"/> 
    </bean> 


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/travelmanagement" /> 
     <property name="username" value="root" /> 
     <property name="password" value="xxxx" /> 
    </bean> 

    <bean id="currentUserDetailsService" class="com.crossover.trial.travelmanagementportal.service.CurrentUserDetailsService" /> 


    <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 


    <bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
     <property name="userDetailsService" ref="currentUserDetailsService" /> 
     <property name="passwordEncoder" ref="encoder" /> 
    </bean> 

</beans> 

回答

1

如果你在你的pom.xml使用

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 

,那麼你可以簡單地設置你的連接db with:

spring.datasource.url= 
spring.datasource.username= 
spring.datasource.password= 
spring.jpa.hibernate.ddl-auto= 

in application.properties

有一個easy way生成一個啓動項目與春季啓動。

+0

我應該在哪裏寫spring.datasource ....在application.properties中? –

+0

文件中的任何位置。文件必須在classpath中:http://stackoverflow.com/questions/38775194/where-is-the-application-properties-in-spring-boot – MicD