Получить EntityManager с Hibernate 4 и базы данных H2.
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;
}
}
Зависимости:
- com.h2database: h2: 1.4.178
- org.hibernate: спящий режим жилы: 4.3.5.Final
- org.hibernate: спящий режим-EntityManager : 4.3.5.Final
Смотрите также http://code.google.com/p/hibernate-generic-dao/issues/detail?id=77 – ripper234 2010-11-27 17:02:55