Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Spring、Hibernate、EntityManager和JPA连接到mysql数据库_Java_Mysql_Spring_Hibernate_Jpa - Fatal编程技术网

Java 如何使用Spring、Hibernate、EntityManager和JPA连接到mysql数据库

Java 如何使用Spring、Hibernate、EntityManager和JPA连接到mysql数据库,java,mysql,spring,hibernate,jpa,Java,Mysql,Spring,Hibernate,Jpa,我正在尝试将Spring、Hibernate、JPA与mysql结合起来。我已经创建了一些模型、DAO和服务对象,并试图通过spring将它们链接在一起。Hibernate正在成功剥离模型,但spring未能创建Hibernate SessionFactory: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ent

我正在尝试将Spring、Hibernate、JPA与mysql结合起来。我已经创建了一些模型、DAO和服务对象,并试图通过spring将它们链接在一起。Hibernate正在成功剥离模型,但spring未能创建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
我的春木看起来像这样:

<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>

org.hibernate.dialogue.mysql5dialogue
验证
真的
有人能告诉我这个配置缺少什么吗?我必须为SpringFactory单独创建一个bean吗?若并没有人知道这个问题的答案,那个么在春天有并没有办法说出SessionFactory无法建立的确切原因呢


更新:原来问题出在JPA配置中

如果使用spring boot,您可以避免所有这些低级配置问题。一旦您运行了它,并且仍然对它感兴趣,您仍然可以尝试了解配置是如何工作的。

如果使用spring boot,您可以避免所有这些低级配置问题。一旦您运行了它,并且您仍然对它感兴趣,您仍然可以尝试了解配置是如何工作的。

您是否在
classpath:META-INF/
中创建了
persistence.xml
?不,如果使用JPA符号您不需要在那里添加实体,则不需要它。只需尝试使用空的
持久性单元
部分创建空的持久性xml配置。不要忘记
持久化单元的set name属性
。我根本不想创建persistence.xml。据我了解,JPA应该足够了。我在SpringXML中添加了这一行:它与持久性xml的作用相同。我已经让spring使用这个配置,它不想这样做的原因是在JPA it中。我在其中一个数据库中组合了密钥,它必须是可序列化的。现在我面临一个新的错误,Spring在我可以用Lazy fetch获取整个记录之前终止了会话。这是一个众所周知的问题。您不能在不同的事务中使用
Lazy
fetch,因为事务结束时会话将关闭。将
Lazy
替换为
Eager
,或使用单独的查询手动获取相关实体。您是否在
classpath:META-INF/
中创建了
persistence.xml
?否,如果使用JPA符号您不需要在其中添加实体,则不需要此项。只需尝试使用空的
持久性单元
部分创建空的持久性xml配置。不要忘记
持久化单元的set name属性
。我根本不想创建persistence.xml。据我了解,JPA应该足够了。我在SpringXML中添加了这一行:它与持久性xml的作用相同。我已经让spring使用这个配置,它不想这样做的原因是在JPA it中。我在其中一个数据库中组合了密钥,它必须是可序列化的。现在我面临一个新的错误,Spring在我可以用Lazy fetch获取整个记录之前终止了会话。这是一个众所周知的问题。您不能在不同的事务中使用
Lazy
fetch,因为事务结束时会话将关闭。用
Eager
替换
Lazy
,或者用单独的查询手动获取相关实体。