简体   繁体   中英

ORM with JPA and Hibernate: EntityManager is null

I am trying to get started with ORM using Hibernate in a Java SE application. I have read that the modern way is to use JPA and then Hibernate as a persistence provider. However, my EntityManager is null:

dependencies:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.10.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.10.Final</version>
    </dependency>

Entities:

annotated with annotations form javax.persistence

To work with the database:

public class PersistenceManager {

    private static EntityManagerFactory emFactory;

    private PersistenceManager() {
        emFactory = Persistence.createEntityManagerFactory("pers-unit"); // defined in a persistence.xml
    }

    public static EntityManager getEntityManager() {
        return emFactory.createEntityManager();
    }

    public static void close() {
        emFactory.close();
    }
}

and then:

    String sql = "SELECT m FROM table m WHERE m.id = :id";
    EntityManager em = PersistenceManager.getEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery(sql);
    q.setParameter("id", key);
    Content result = (Content) q.getSingleResult();
    em.getTransaction().commit();
    em.close();
    PersistenceManager.close();
    return result;

persistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"

    version="2.1">

    <persistence-unit name="pers-unit"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>

            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/dbdb" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>
</persistence>

Is this the way to got now? Most tutorials I see use the SessionFactory and transaction from hibernate.

I looked at a book from 2014 and it uses this:

Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder();
srBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);

Which already has some deprecated code.

What's the way to do this?

I think you have some error in your PersistenceManager class. The EntityManagerFactory member is static, so is the method to obtain the EntityManager but as you invoke the method in a static way, you don't have an instance of PersistenceManager. This means the constructor is not called at all and thus you won't be able to call createEntityManager() on the null reference.

You could try this one:

public class PersistenceManager {

    private static EntityManagerFactory emFactory;

    static {
        emFactory = Persistence.createEntityManagerFactory("pers-unit"); // defined in a persistence.xml
    }

    public static EntityManager getEntityManager() {
       return emFactory.createEntityManager();
    }

    public static void close() {
        emFactory.close();
    }
}

Concerning your question if this is the way to go... There is not definitive answer as this depends on a personal choice. The tutorials that use SessionFactory and the like refer to Hibernate while those that name the EntityManagerFactory and the EntityManager refer to the JPA spec (which Hibernate implements but it also has its non-JPA API).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM