EntityManager nullPointerEX Maven项目+休眠

时间:2018-10-03 09:04:44

标签: java postgresql hibernate maven jdbc

我在一个简单的hibernate + maven项目中,我正在尝试使用@PersistenceUnit批注来管理EntityManagerFactory(事务类型为“ RESOURCE_LOCAL”),但是在.createEntityManager()上会发生NullPointerException ... 从jsps到servlet的每件事都起作用,我的问题只是这个EM,我不知道为什么

我用来管理EM的班级

package weblibrary.util;


 public class DBConnector {


//@PersistenceContext(unitName = "Biblioteca")
private EntityManager em;

// emf = Persistence.createEntityManagerFactory("Biblioteca");

@PersistenceUnit(unitName = "Biblioteca")
private EntityManagerFactory emf;

public EntityManager beginConnection() {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    return em;
}

public EntityManager getEntityManager() {

    return this.em;
}

}

这是我使用此“连接器”的位置(在尝试使用@PersitenceUnit批注之前编写了代码)

    public void addBook(String isbn, String author, String title, String year, int quantity) {
    DBConnector conn = new DBConnector();
    conn.beginConnection();
    Boolean checkQuantity = conn.getEntityManager().createNativeQuery("select * from Book where isbn='" + isbn + "'")
            .getResultList().isEmpty();
    if (checkQuantity == true) {
        Book book = new Book();
        book.setIsbn(isbn);
        book.setAuthor(author);
        book.setTitle(title);
        book.setYear(year);
        book.setQuantity(quantity);
        conn.getEntityManager().persist(book);

    } else {
        Query query = conn.getEntityManager().createNativeQuery(
                "update Book  set quantity = quantity + " + quantity + " where isbn='" + isbn + "'");
        query.executeUpdate();

    }

    conn.getEntityManager().getTransaction().commit();
    conn.getEntityManager().close();
    System.out.println("\n\n Book Details Added \n");
}

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
   <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="Biblioteca"  transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

  <!--  <jta-data-source>java:jboss/datasources/New_PostgreSQL</jta-data-source> -->

    <class>weblibrary.entities.Book</class>
    <class>weblibrary.entities.User</class>

    <properties>        
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" /> <!-- BD Mane -->
        <property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
        <property name="javax.persistence.jdbc.password" value="admin" /> <!-- DB Password -->

        <!-- <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>  -->

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/> <!-- DB Dialect -->
        <property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->

        <property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
        <property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->

    </properties>

</persistence-unit>

java.lang.NullPointerException
at weblibrary.util.DBConnector.beginConnection(DBConnector.java:21)

on:EntityManager em = emf.createEntityManager();

我正处于学习阶段,也许我缺少一些容易为专家所注意到的东西,希望您能提供帮助,谢谢!

0 个答案:

没有答案