Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA connection with H2 database

Tags:

hibernate

jpa

h2

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

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

"Unable to create EntityManagerFactory"

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

. Can anyonw provide me the working persistence.xml?

like image 740
Gnanam Avatar asked Aug 06 '11 09:08

Gnanam


People also ask

How do I connect to my H2 memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .

What is the use of H2 database in spring boot?

Spring Boot & H2 - Overview H2 database is an open source, embedded and in memory relational database management system. It is written in Java and provides a client/server application. It stores data in system memory instead of disk. Once program is closed, data is also lost.

What is JPA H2?

H2 is an open-source in-memory SQL database written in Java. It can be embedded in Java applications or use as a standalone database in client-server mode. An in-memory database is created when the application starts up and destroyed when the application shuts down.


1 Answers

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>
like image 61
Derek Avatar answered Sep 19 '22 19:09

Derek