Storing Entity objects with the EntityManager API gives us a convenient way to store objects with relationships. You may have noticed that we are somewhat limited in how we find Entities if we rely on the EntityManager interface. The find method only accepts a primary key to search for in our objects. What if we don't know the primary key of the object or objects we want to find? We turn to the Query interface. The Query interface gives us a way to search an ObjectGrid instance using an SQL-like language to find managed entities that meet certain criteria.
Our first work with Query will be very simple:
private void queryBatch() { Query query = em.createQuery("select b.name from Batch b"); String batchName = (String)query.getSingleResult(); System.out.println("Batch name is: " + batchName); }
We start working with the Query interface by creating a Query from the EntityManager#createQuery(String queryStr)
method. The queryStr
argument is an ObjectGrid Query Language query that...