1. The JPQL statement query code is as follows:
@ Testpublic void query () {EntityManagerFactory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); // name parameter Query or bit parameter query Query = em. createQuery ("select p from Person p. id =? 1 "); query. setParameter (1, 1); // call the getSingleResult () method. Results are required (that is, the database must have corresponding data). Otherwise, the Person person = (Person) is saved) query. getSingleResult (); System. out. println (person. getName (); em. close (); factory. close ();}
2. Delete the query. The code is as follows:
@ Testpublic void deletequery () {EntityManagerFactory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin (); // name parameter Query or bit parameter query Query query = em. createQuery ("delete from Person p where p. id =? 1 "); query. setParameter (1, 1); query.exe cuteUpdate (); em. getTransaction (). commit (); em. close (); factory. close ();}
3. Update the query. The code is as follows:
@ Testpublic void updatequery () {EntityManagerFactory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin (); // name the parameter Query query = em. createQuery ("update Person p set name =: name where p. id =: id "); query. setParameter ("name", "hwl"); query. setParameter ("id", 3); query.exe cuteUpdate (); em. getTransaction (). commit (); em. close (); factory. close ();}
4. Detailed code:
Package learn. jpa. junit. test; import static org. junit. assert. *; import javax. persistence. entityManager; import javax. persistence. entityManagerFactory; import javax. persistence. persistence; import javax. persistence. query; import learn. jpa. bean. person; import org. junit. test; public class PersonTest {@ Test public void save () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa "); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin (); // start the transaction em. persist (new Person ("hwl"); em. getTransaction (). commit (); em. close (); factory. close ();}/*** query */@ Test public void getPerson () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); // The first parameter indicates the object class. The second parameter indicates the object identifier Person person. = Em. find (Person. class, 1); System. out. println (person. getName (); em. close (); factory. close () ;}@ Test public void getPerson2 () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); // The first parameter indicates the entity class. The second parameter indicates the entity identifier. // when this method is called, data is not obtained from the database immediately, only returns a proxy object Person = em. getReference (Person. class, 1); // when we access the attributes of the proxy object To obtain data from the database. // if the above method is executed, there is no corresponding data in the database and no error will be reported. The person will be executed. system. out. println (person. getName (); em. close (); factory. close () ;}@ Test public void updatePerson () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); // The Entity Manager has been associated with em. getTransaction (). begin (); // after finding out, the person object is hosted on the CPU. // at this time, the set object of the object class is called. Method, you can change the data, but it is not immediately changed, it is placed in the JDBC batch processing transaction Library, Person person = em. find (Person. class, 1); person. setName ("zxx"); // after the transaction is committed, the data will be synchronized to the database em. getTransaction (). commit (); em. close (); factory. close (); // The entity has four states in the CPU // The first creates (new) the second manages (manage) third, free, fourth, delete} @ Test public void updatePerson2 () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. cres AteEntityManager (); em. getTransaction (). begin (); Person person = em. find (Person. class, 1); em. clear (); // changes the state of all entities in the entity manager to the Free State person. setName ("zxx"); em. merge (person); // synchronize data updates in the Free State to the database em. getTransaction (). commit (); em. close (); factory. close () ;}@ Test public void delete () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntity Manager (); em. getTransaction (). begin (); Person person = em. find (Person. class, 1); em. remove (person); em. getTransaction (). commit (); em. close (); factory. close () ;}@ Test public void query () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); // name parameter Query or bit parameter query Query = em. createQuery ("select p from Person p. id =? 1 "); query. setParameter (1, 1); // call the getSingleResult () method. Results are required (that is, the database must have corresponding data). Otherwise, the Person person = (Person) is saved) query. getSingleResult (); System. out. println (person. getName (); em. close (); factory. close () ;}@ Test public void deletequery () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin () ; // Query query = em. createQuery ("delete from Person p where p. id =? 1 "); query. setParameter (1, 1); query.exe cuteUpdate (); em. getTransaction (). commit (); em. close (); factory. close () ;}@ Test public void updatequery () {EntityManagerFactory factory = Persistence. createEntityManagerFactory ("learn_jpa"); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin (); // name the parameter Query query = em. createQuery ("update Person p set name =: name where p. id =: id "); query. setParameter ("name", "hwl"); query. setParameter ("id", 3); query.exe cuteUpdate (); em. getTransaction (). commit (); em. close (); factory. close ();}}
JPA learning-Section 8: using JPQL statements for queries