2

I'm using hibernate but not Spring, and just found hibernate-generic-dao. The concept seems nice, but when I run it I get a NPE because I haven't called setEntityManager().

How do I obtain an EntityManager without using Spring?

ripper234
  • 222,824
  • 274
  • 634
  • 905

2 Answers2

1

Obtain EntityManager with Hibernate 4 and H2 database.

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
import org.hibernate.service.ServiceRegistry;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitTransactionType;

public class Main {

    public static void main(String[] args) {
        Configuration configuration = getConfiguration();

        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

        EntityManagerFactory factory = new EntityManagerFactoryImpl(
                PersistenceUnitTransactionType.RESOURCE_LOCAL, true, null, configuration, serviceRegistry, null);

        EntityManager em = factory.createEntityManager();
    }

    private static Configuration getConfiguration() {
        Configuration configuration = new Configuration();
        configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
        configuration.setProperty("hibernate.connection.url", "jdbc:h2:~/test");
        configuration.setProperty("hibernate.connection.pool_size", "1");
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        configuration.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.internal.NoCachingRegionFactory");
        configuration.setProperty("hibernate.show_sql", "true");
        configuration.setProperty("hibernate.hbm2ddl.auto", "create");
        configuration.setProperty("hibernate.connection.autocommit", "false");
        configuration.addAnnotatedClass(RegionEntity.class);
        return configuration;
    }
}

Dependencies:

  1. com.h2database:h2:1.4.178
  2. org.hibernate:hibernate-core:4.3.5.Final
  3. org.hibernate:hibernate-entitymanager:4.3.5.Final
Aleks Ya
  • 859
  • 5
  • 15
  • 27
1

I have this is some test code. It looks for a persistence.xml file in the META-INF directory.

EntityManagerFactory emf=Persistence.createEntityManagerFactory("test-unit");
EntityManager em=emf.createEntityManager();

Here's an example persistence.xml that uses hibernate connected to a postgresql database and two entity classes:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.0"  
             xmlns="http://java.sun.com/xml/ns/persistence"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/per\
sistence_1_0.xsd"> 
  <persistence-unit name="test-unit" transaction-type="RESOURCE_LOCAL"> 
    <class>com.example.package.Entity1</class> 
    <class>com.example.package.Entity2</class> 
    <properties> 
      <property name="hibernate.dialect"  
                value="org.hibernate.dialect.PostgreSQLDialect"/> 
      <property name="hibernate.connection.driver_class"  
                value="org.postgresql.Driver"/> 
      <property name="hibernate.connection.username" value="login"/> 
      <property name="hibernate.connection.password" value="password"/> 
      <property name="hibernate.connection.url"  
                value="jdbc:postgresql://dbserver.internal:5432/dbname"/> 
    </properties> 
  </persistence-unit> 
</persistence> 
JOTN
  • 6,120
  • 2
  • 26
  • 31
  • Amm ... I don't have a persistence.xml file. – ripper234 Nov 27 '10 at 17:18
  • You have to create one. That's what's going to define your entity classes. – JOTN Nov 27 '10 at 17:20
  • No, it isn't. I'm using a hibernate.cfg.xml, and it doesn't look like what you posted, and my tests are working :) Are you saying that EntityManager has to have this XML? If this is the case, then probably hibernate-generic-dao is not for me at this point. – ripper234 Nov 27 '10 at 17:46
  • `hibernate.cfg.xml` is for native Hibernate. `persistence.xml` is for JPA. If you are going to use `EntityManager`, you should probably use `persistence.xml`, since that's the JPA approach. If you just want to use Hibernate, then use `SessionFactory` and `Session` instead. – Steven Benitez Nov 27 '10 at 17:49
  • @Steven - I was using SessionFactory and Session just fine, but I wanted to integrate hibernate-generic-dao, and apparently it needs EntityManager. – ripper234 Nov 27 '10 at 21:41