我坚持使用spring.config,persistence.xml和pom文件的不同组合

时间:2017-02-18 07:13:14

标签: java mysql hibernate spring-boot

我正在为hibernate 4和mysql的spring boot应用程序寻找一个可用的配置版本。

自昨天以来,我的配置出现以下错误:(:

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory'的bean在类路径中定义   resource [spring-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 个答案:

答案 0 :(得分:1)

如果您使用

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
在你的pom.xml中

然后你可以简单地设置你与db的连接:

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

在application.properties

有一个easy way来生成一个带有spring boot的启动项目。