JPA Query



Query is an interface, which is used to create object of EntityManager. It is exist in javax.persistence package
Ex: EntityManager em = emf.createEntityManager();

Methods:
1. javax.persistence.EntityManager createEntityManager()
   It is used to create instance of EntityManager.
2. boolean isOpen()
   It is used to check whether EntityManagerFactory is open or not.
3. void close()
   It is used to close EnityManagerFactory.
4. javax.persistence.Cache getCache()
   It is used find Cache instance.

Other Methods:
5. javax.persistence.metamodel.Metamodel getMetamodel()
6. javax.persistence.PersistenceUnitUtil getPersistenceUnitUtil()
7. Map<String, Object> getProperties()
8. javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()
9. javax.persistence.EntityManager createEntityManager(Map map)


Run this program, then JPA will create a table with name emp and 2 columns with name id and name. It will also create a sequence with name emp_seq.
Insert one row into table emp with id 1 and name Ravindra.
Main.java

package com.groupofknowledge;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
public static void main(String[] args) {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("demo");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
Emp e=new Emp();
e.setName("Ravi");
em.persist(e);
em.getTransaction().commit();
}
}


Result...

emp (Table)
id name
1 Ravindra

Download Demo