问题
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");
回答1:
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);
来源:https://stackoverflow.com/questions/16876681/read-data-from-jpa-the-ways-to-do-it