Reputation: 1703
I read in the documentation and i see that to read the jpa data i need to use the following code. this is the only way to read the data from the JPA tables via sql?
factory = Persistence.createEntityManagerFactory("abc");
EntityManager entityManager = factory.createEntityManager();
Query query = entityManager.createQuery("SELECT p FROM " + className + " p");
Upvotes: 0
Views: 6203
Reputation: 11984
There are several ways to read data using JPA. The example you provided is using JPQL. Additionally, you can:
execute a native SQL query via EntityManager#createNativeQuery(String nativeSql)
:
Query q = entityManager.createNativeQuery("SELECT * FROM MY_TABLE");
use the Criteria API
retrieve a single entity by it's primary key using EntityManager#find(...)
:
MyObject myObject = entityManager.find(MyObject.class, myObectId);
Upvotes: 2