160 likes | 370 Views
JPA. Overview della tecnologia. Java Persistence Architecture. Definita nella JSR-220 EJB 3.0 ( www.jcp.org ). Tentativo di unificare EJB2.1 con JDO (Java Data Object). Java Persistence API (JPA) permette di mappare POJO in dati relazionale. API standard per: il salvataggio dei dati
E N D
JPA Overview della tecnologia
Java Persistence Architecture • Definita nella JSR-220 EJB 3.0 (www.jcp.org). • Tentativo di unificare EJB2.1 con JDO (Java Data Object). • Java Persistence API (JPA) permette di mappare POJO in dati relazionale. • API standard per: • il salvataggio dei dati • la loro consultazione • Cambiamento cancellazione.
JPA • I dati persistenti vengono chiamti Entity. • Per esempio User puo’ essere una entity. • I dati dell’utente sono dati permanenti: • Name • Surname • Email
Entity Example @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private String surname; private String email; public User(String name, String surname, String email) { super(); this.name = name; this.surname = surname; this.email = email; } }
JPA • Si basa sul concetto di Cache. • Flush dei dati della Cache nel database per modificare e inserire i dati nel database. • In JPA il PersistenceContext e’ una Cache. • Un EntityManager gestisce un PersistenceContext (e quindi una cache). • Un EntityManagerFactory ci permette di creare una cache.
Come ottenere un EntityManager public class EMF { private static final EntityManagerFactory emfInstance = Persistence.createEntityManagerFactory("tutorial"); private EMF() {} public static EntityManagerFactory get() { return emfInstance; } } Nella Servlet: EMF.get().createEntityManager()
Aggiungere un Utente if (action.compareToIgnoreCase("addUser") == 0) { // Performs some checks could be done by javascript String name = req.getParameter("name"); String surname = req.getParameter("surname"); String email = req.getParameter("email"); if (name != null && surname != null && email != null) { User user = new User(name, surname, email); try { em.getTransaction().begin(); em.persist(user); em.getTransaction().commit(); out.println("SAVED USER: name=" + name + " surname=" + " email=" + email); } finally { em.close(); } } }
Visualizzare tutti gli utenti if (action == null) { Query query = em .createQuery("SELECT u FROM it.unige.dist.appengine.entity.User u"); List<User> users = query.getResultList(); for (User user : users) { out.println("USER: name=" + user.getName() + “surname=“ + user.getSurname() + " email=" + user.getEmail()); } }
Ricerca sugli utenti if (action.compareTo("searchByEmail")==0) { String email = req.getParameter("email"); if (email != null) { Query query = em .createQuery("SELECT u FROM it.unige.dist.appengine.entity.User u WHERE u.email=:email"); query.setParameter("email", email); List<User> users = query.getResultList(); for (User user : users) { out.println("USER: name=" + user.getName() + " surname=" + user.getSurname() + " email=" + user.getEmail()); } } }
Rimuovere Utenti em.getTransaction().begin(); User e = em.find(User.class, Long.valueOf(id)); em.remove(e); em.getTransaction().commit();
Esempio di persistence.xml per Jboss e MySQL. <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name=”tutorial" transaction-type="RESOURCE_LOCAL"> <properties> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.username" value=”user" /> <property name="hibernate.connection.password" value=”pwd" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test" /> </properties> </persistence-unit> </persistence>
Entity Example 2 @Entity @Table(WEB_USERS) public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name=“NOME”, nullable=false, length=70)) private String name; @Column(name=“COGNOME”, nullable=false, length=70)) private String surname; @Column(name=“EMAIL”, nullable=false, length=70)) private String email; public User(String name, String surname, String email) { super(); this.name = name; this.surname = surname; this.email = email; } … // setters & getters … }
Riferimenti • Introduction to Java Persistence API(JPA): http://www.javabeat.net/articles/5-introduction-to-java-persistence-apijpa-1.html • The Java EE 5 Tutorial: http://download.oracle.com/javaee/5/tutorial/doc/