JPA与H2数据库的连接 [英] JPA connection with H2 database

查看:146
本文介绍了JPA与H2数据库的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JPA项目开发一个hibernate,并试图通过使用H2(嵌入式数据库)来获得工作的persistence.xml。

I am developing a hibernate with JPA project and trying to get the working persistence.xml by using H2(Embedded database).

Persistence.xml

Persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="1.0">

    <persistence-unit name="DefaultPersistenceUnit"
        transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>entity.user</class>
        <properties>
           <property name="hibernate.connection.url" value="jdbc:h2:/~test" /> -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.password" value="admin" />
            <property name="hibernate.connection.username" value="admin" />
        </properties>
    </persistence-unit>
</persistence>

但我的工作不正常,我总是收到错误

But my this is not working, always I am getting the error


无法创建EntityManagerFactory

"Unable to create EntityManagerFactory"



EntityManagerFactory emf = Persistence.createEntityManagerFactory("DefaultPersistenceUnit");

。可以anyonw为我提供工作的persistence.xml吗?

. Can anyonw provide me the working persistence.xml?

推荐答案

您是否使用Spring?如果是这样的话,只要EntityManagerFactory不是由容器提供的,就可以尝试此操作。

Are you using Spring? If so you could try this as long as the EntityManagerFactory is not provided by a container

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="DefaultPersistenceUnit" />
</bean>

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

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:database/~test" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

这篇关于JPA与H2数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆