Advertisement
disiodj

siw-jpa-es1-MainClass

May 20th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. package it.uniroma3.db.products;
  2.  
  3. import javax.persistence.EntityManager;
  4. import javax.persistence.EntityManagerFactory;
  5. import javax.persistence.EntityTransaction;
  6. import javax.persistence.Persistence;
  7.  
  8. public class ProductMain {
  9.    
  10.     public static void main(String[] args) {
  11.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("products-unit");
  12.         EntityManager em = emf.createEntityManager(); //è una classe che gestisce le entità
  13.  
  14.         Product product = new Product();
  15.         Product product2 = new Product();
  16.         product.setName("KRIDDIG");
  17.         product.setPrice(3.5F);
  18.         product.setDescription("A wonderful bla bla");
  19.         product.setCode("9781853262715-AA");
  20.  
  21.         EntityTransaction tx = em.getTransaction(); //il manager crea un oggetto di transizione.
  22.         tx.begin(); //la transazione sul database viene iniziata.
  23.         em.persist(product);
  24.         tx.commit();
  25.        
  26.         product2.setName("jbl");
  27.         product2.setPrice(4F);
  28.         product2.setDescription("come imparare ad amare");
  29.         product2.setCode("333111-AA");
  30.         tx.begin(); //Questo permette di eseguire la transizione.
  31.         em.persist(product2);
  32.         tx.commit();
  33.         em.close();
  34.         emf.close();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement