ripper234
ripper234

Reputation: 230028

Obtaining EntityManager without Spring

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?

Upvotes: 2

Views: 3395

Answers (2)

Aleks Ya
Aleks Ya

Reputation: 909

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

Upvotes: 1

JOTN
JOTN

Reputation: 6317

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> 

Upvotes: 1

Related Questions