Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.uniroma3.db.products;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.Persistence;
- public class ProductMain {
- public static void main(String[] args) {
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("products-unit");
- EntityManager em = emf.createEntityManager(); //è una classe che gestisce le entità
- Product product = new Product();
- Product product2 = new Product();
- product.setName("KRIDDIG");
- product.setPrice(3.5F);
- product.setDescription("A wonderful bla bla");
- product.setCode("9781853262715-AA");
- EntityTransaction tx = em.getTransaction(); //il manager crea un oggetto di transizione.
- tx.begin(); //la transazione sul database viene iniziata.
- em.persist(product);
- tx.commit();
- product2.setName("jbl");
- product2.setPrice(4F);
- product2.setDescription("come imparare ad amare");
- product2.setCode("333111-AA");
- tx.begin(); //Questo permette di eseguire la transizione.
- em.persist(product2);
- tx.commit();
- em.close();
- emf.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement