Read Data from JPA the ways to do it

怎甘沉沦 提交于 2019-12-25 07:37:56

问题


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:

  1. execute a native SQL query via EntityManager#createNativeQuery(String nativeSql):

    Query q = entityManager.createNativeQuery("SELECT * FROM MY_TABLE");
    
  2. use the Criteria API

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!