我使用的是 hibernate 但不是 Spring,只是找到了hibernate-generic-dao。这个概念看起来不错,但是当我运行它时,我得到了一个 NPE,因为我没有调用 setEntityManager()。
如何在不使用 Spring 的情况下获取 EntityManager?
我使用的是 hibernate 但不是 Spring,只是找到了hibernate-generic-dao。这个概念看起来不错,但是当我运行它时,我得到了一个 NPE,因为我没有调用 setEntityManager()。
如何在不使用 Spring 的情况下获取 EntityManager?
使用 Hibernate 4 和 H2 数据库获取 EntityManager。
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;
}
}
依赖项:
我有这是一些测试代码。它在 META-INF 目录中查找 persistence.xml 文件。
EntityManagerFactory emf=Persistence.createEntityManagerFactory("test-unit");
EntityManager em=emf.createEntityManager();
这是一个使用 hibernate 连接到 postgresql 数据库和两个实体类的示例 persistence.xml:
<?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>