10

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?

3
  • 2
    Does the error message only say that? Don't you have a more detailed message and/or stack trace? Is your class really named entity.user? Commented Aug 6, 2011 at 9:58
  • 1
    Is your hibernate.connection.url really jdbc:h2:/~test? jdbc:h2:file:~/test;IFEXISTS=TRUE seems more plausible. Commented Aug 7, 2011 at 5:35
  • 1
    What version of Hibernate? Commented Aug 7, 2011 at 5:42

1 Answer 1

11

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

I prefer this approach because it is easier to switch beans out for tests or other configurations (web or standalone) than switching out the persistence.xml.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.