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;
    }
#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
ObjectDB - Fast Object Database for Java (JPA/JDO)
#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?

#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
ObjectDB - Fast Object Database for Java (JPA/JDO)
#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;
    }
#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
ObjectDB - Fast Object Database for Java (JPA/JDO)
#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...

#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
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login