JPA EntityManager persist method does not persist the object to db, but the object has an id after saving

I am trying to save an object to DB using jpa. Here is my essence:

import javax.persistence.*;

@Entity
public class Book {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    public Book() {
    }
    public Book(String title) {
        this.title = title;
    }
    public Long getId() {
        return id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

      

Here's the persistence.xml:

<persistence-unit name="bookUnit">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>ee.jpa.Book</class>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Lessons"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="1"/>
    </properties>
</persistence-unit>

      

Here is my EJB:

@Stateless
public class JpaBean {
    @PersistenceContext
    EntityManager entityManager;
    public long saveBook(Book book) {
        entityManager.persist(book);
        return book.getId();
    }
}

      

Here is my servlet:

@WebServlet("/jpaExample")
public class ServletExample extends HttpServlet {
    @EJB
    JpaBean jpaBean;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = new Book("servlet");
        long l = jpaBean.saveBook(book);
        resp.getWriter().write(l + " ");
    }
}

      

I added EclipseLink.jar to my project and to the TomcatEEDir / lib / directory. Everything compiles and runs without exception. I get different numbers every time I run the servlet, like 1 2 3 .... etc. But when I look at the db there is nothing there. No table, no data. What have I done wrong? How to fix it? Also I tried using java SE, for that I added transactional type = "RESOURCE_LOCAL" to persistence block. And I wrote the code like this:

public class MainExample {
public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("bookUnit");
    EntityManager em = emf.createEntityManager();
    Book book = new Book("main");
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(book);
    tx.commit();
    em.close();
    emf.close();
}
}

      

This code works fine and saves data to DB. So what happened to my previous code. Why nothing saves the DB?

Edit: Same result if using Hibernate.

+3


source to share


1 answer


I think you need to call .flush () for them to be saved. Try it, I think it will work. also use



@Transactional
public long saveBook(Book book) {
    entityManager.persist(book);
    entityManager.flush();
    return book.getId();
}

      

0


source







All Articles