1

i'm in a simple hibernate+maven project, i'm trying to use @PersistenceUnit annotation to manage the EntityManagerFactory (transaction type is "RESOURCE_LOCAL") , but on .createEntityManager() it goes on NullPointerException... every things works from the jsps to the servlets, my problem is just this EM and i cant figure out why

The class i use to manage the 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;
}

}

this is the where i use this "connector" (the code is written before i tried to use the @PersitenceUnit annotation )

    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");
}

My 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();

i'm in a learning phase, maybe i'm missing something that is easy to notice for experts, hope you can help, thanks!

Zappa
  • 25
  • 8
  • It means that `emf` is `null`. Why that is depends on how you are running and initializing your application. The `@PersistenceUnit` annotation only works when you're using CDI to inject dependencies. – Mark Rotteveel Oct 03 '18 at 11:19

0 Answers0