不使用Spring获取EntityManager

发布于 2024-10-04 16:41:05 字数 239 浏览 5 评论 0原文

我使用的是 hibernate,但不是 Spring,刚刚发现 hibernate-generic-dao。 这个概念看起来不错,但是当我运行它时,我得到一个 NPE,因为我没有调用 setEntityManager()。

如何在不使用 Spring 的情况下获取 EntityManager?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-10-11 16:41:05

我有这是一些测试代码。它在 META-INF 目录中查找 persistence.xml 文件。

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

下面是一个示例 persistence.xml,它使用连接到 postgresql 数据库的 hibernate 和两个实体类:

<?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> 

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> 
英雄似剑 2024-10-11 16:41:05

使用 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;
    }
}

依赖项:

  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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文