1 / 14

JPA

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

trevet
Download Presentation

JPA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JPA Overview della tecnologia

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

  3. JPA • I dati persistenti vengono chiamti Entity. • Per esempio User puo’ essere una entity. • I dati dell’utente sono dati permanenti: • Name • Surname • Email

  4. 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; } }

  5. ORM

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

  7. 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()

  8. 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(); } } }

  9. 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()); } }

  10. 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()); } } }

  11. Rimuovere Utenti em.getTransaction().begin(); User e = em.find(User.class, Long.valueOf(id)); em.remove(e); em.getTransaction().commit();

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

  13. 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 … }

  14. 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/

More Related