ObjectDB ObjectDB

ObjectDB CRUD Examples

#1

Would this be correct?

    public List<Foo> read() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
            getServletContext().getInitParameter("database"));
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
        Root<Foo> root = criteriaQuery.from(Foo.class);
        List<Foo> allFoo = (List<Foo>) criteriaQuery.select(root);
        em.getTransaction().commit();
        em.close();
        emf.close();
        return allFoo;
    }
edit
delete
#2

CRUD operations are explained and demonstrated in these manual pages.

We do not have specific NetBeans examples in addition to the tutorials.

ObjectDB Support
edit
delete
#3

The only way to do this one:
 

public List<Foo> read() {}

Is by using a Query? i.e. Query query = em.createQuery("SELECT foo FROM Foo foo");

There's no em.selectAll method or something similar?

edit
delete
#4

Yes, you have to run a query in order to retrieve all the instances of a class.

You may use a criteria query instead of a string based query, but it will not save you lines of code.

ObjectDB Support
edit
delete
#5

Would this be correct?

    public List<Foo> read() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
            getServletContext().getInitParameter("database"));
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
        Root<Foo> root = criteriaQuery.from(Foo.class);
        List<Foo> allFoo = (List<Foo>) criteriaQuery.select(root);
        em.getTransaction().commit();
        em.close();
        emf.close();
        return allFoo;
    }
edit
delete
#6

You can try and see if it works.

It would be more efficient to avoid creating and closing EntityManagerFactory instances for every operation (which is very inefficient). In addition, you do not need a transaction for running a query.

After closing the EntityManager, only data that was eagerly fetched will be available.

ObjectDB Support
edit
delete
#7

So what would be an efficient solution to these CRUD operations in terms of EMF and Transactions? I'm finding these examples: http://www.objectdb.com/java/jpa/persistence/crud difficult as they don't really give a full example of what to do. This makes it difficult as this is really the only place to start...

edit
delete
#8

Please consider reading the entire manual. It would probably take several hours but may save later much more. This is especially important if you do not have prior experience with JPA.

For example a few pages before the CRUD section on this page you could read that: "The instantiation of the EntityManagerFactory itself might be less efficient, but it is a one time operation. Once constructed, it can serve the entire application."

This is also demonstrated in the tutorials, which are small but full application examples.

But these tutorials cannot replace the manual, which has to be read.

ObjectDB Support
edit
delete

Reply

To post on this website please sign in.