如何使用Spring,Hibernate,EntityManager和JPA

时间:2016-02-21 06:38:29

标签: java mysql spring hibernate jpa

我试图将Spring,Hibernate,JPA与mysql结合起来。我已经创建了一些模型,DAO和服务对象,并且我试图通过spring链接在一起。 Hibernate正在削减模型成功,但春天未能创建Hibernate SessionFactory:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class path resource [spring/config/Spring-Config.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

我的春天woodoo看起来像这样:

<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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Enable annotation configuration -->
    <context:annotation-config />

    <!-- Scan JDBC Implementations -->
    <context:component-scan base-package="org.rdswitchboard.linkers.neo4j.web.researcher.dao.jdbc.impl" />

    <!-- Scan Service Implementations -->
    <context:component-scan base-package="org.rdswitchboard.linkers.neo4j.web.researcher.service.impl" />

    <!-- Add properties file -->
    <context:property-placeholder location="classpath:properties/jdbc.properties"/>

    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="org.rdswitchboard.linkers.neo4j.web.researcher.model" />

        <!-- JpaVendorAdapter implementation for Hibernate EntityManager.
             Exposes Hibernate's persistence provider and EntityManager extension interface -->
        <property name="jpaVendorAdapter"> 
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

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

    <tx:annotation-driven />

</beans>

有没有人能告诉我这个配置缺少什么?我是否必须为SpringFactory单独创建一个bean?如果没有人知道答案,Spring是否有办法确切说明为什么无法构建SessionFactory?

更新:事实证明问题出在JPA配置

1 个答案:

答案 0 :(得分:0)

如果使用spring boot,可以避免所有这些低级别配置问题。一旦你运行它,并且你仍然对它感兴趣,你仍然可以尝试找出配置的工作原理。