Upload
balzofaye
View
180
Download
2
Embed Size (px)
Citation preview
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 1/8
votre recherche OK
accueil du site d'Objis
Promotion formation du mois
Société |Clients |40 Formations |200 tutoriaux |5 centres |Tests |Contactez-nous |
ARCHITECTURE JAVAARCHITECTURE SOAPERFORMANCE JAVA J2EESECURITE JAVAFUSE ESB SERVICEMIX DEV.FUSE ESB SERVICEMIX ADMIN.ACTIVEMQ 5.6 ADMINISTRATIONACTIVEMQ 5.6 AVANCECAMEL 2.9
SELENIUMTESTS JAVAINTEG. CONTINUECONCEPTION UML
DESIGN PATTERNLANGAGE XMLMAVEN 3.04ANT 1.8JBOSS 4/5/6
JBOSS 7TOMCAT 7GLASSFISH 3.1JONAS 5.3WEBSHRERE 8
JAVASCRIPT 1.6JQUERY 1.7AJAXGWT 2.4FLEX 4EXTJS 4.1EXTJS AVANCESPRING 3.1 (5j)
JAVA 7JEE 6WEBSERVICESWEBSERVICES AVJSF 2STRUTS 1STRUTS 2HIBERNATE / JPAEJB 3SENCHA TOUCH 2
ANDROID
Tutoriel JPA : votre première application JPA
Mettez en œuvre la spécification JPA (JSR 317) en utilisant Hibernate comme fournisseur de persistence. Créez votre
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 2/8
première entité JPA (Formation.java). Configurez hibernate à travers le fichier persistence.xml. Utilisez les objetsEntitiManagerFactory, EntityManager et Transaction afin de créer un enregistrement en base de donnée à partir d’un objetFormation instancié. Enfin, mettez en évidence la souplesse de JPA en remplaçant Hibernate par EclipseLink commefournisseur de persistance.
Ce tutoriel est extrait de séances pratiques de la formation HIBERNATE dispensée par Objis.
Prérequis, outils et versions
Installation Hibernate
Liens utiles
+ de 100 tutoriaux java/jee Objis
Tutoriaux HIBERNATE Objis Objis, spécialiste formation java depuis 2005 Site hibernate (javadoc , FAQ)
Objectifs
Voir JPA core en action dans une application java simple Comprendre les étapes de création d’un projet JPA Comprendre comment rendre une classe Java persistante par JPA Comprendre les classes & interfaces EntitiManagerFactory, EntitiManager, EntityTransaction Comprendre le fichier de configuration de JPA : persistence.xml
Programme
Introduction : la spécification JPA Partie 1 : Création projet Partie 2 : Préparation jpa/hibernate (fichier config, librairies) Partie 3 : Préparation Base de données MySQL Partie 4 : JPA/Hibernate en action Partie 5 : Vérification enregistrement DB Partie 6 : changement de fournisseur de persistance (EclipseLink)
Durée
30 min.
Qui sommes-nous ?
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 3/8
Introduction : comprendra la spécification JPA
QUESTION : qu’est ce que JPA ?
QUESTION : quelle différence entre JPA et HIBERNATE ?
Analysez les documents disponibles à l’URL suivante : http://download.oracle.com/otndocs/jcp/persistence-2.0-fr-eval-oth-JSpec/
tutoriel-jpa-hibernate-1-specs-jsr-jpa-2
Expliquez.
Partie 1 : Création projet hibernate
Méthodologie
Domaine métier
Configuration JPA/hibernate
Démarrer la base
Lancer le programme
Vérification base de données
Mise en oeuvre
Dans cette partie vous allez mettre en œuvre la spécification JPA , avec Hibernate comme fournisseur de persistance, à travers un projet
Java Eclipse.
projet Eclipse final
Voici l’arborescence vers laquelle nous allons...
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 4/8
tutoriel-jpa-hibernate-2-projet-eclipse
Le projet Eclipse est le suivant :
demojpa
Classe persistante
Analysez le code de la classe Formation.java
tutoriel-jpa-hibernate-3-entite
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 5/8
Expliquez.
Partie 2 : Configuration JPA/Hibernate
fichier de configuration
Analysez le code du fichier META-INF/persistence.xml
tutoriel-jpa-hibernate-4-configuration-persistence-xml
les librairies
Nous travaillons ici avec la version Hibernate 3.6.9
Les jars nécessaires sont :
— Le fichier hibernate3.jar (à la racine du zip Hibernate) — les jars ’obligatoires’ d’hibernate présents dans lib/required — le jar présent dans le répertoire lib/jpa du zip hibernate - ici hibernate-jpa-2.0-api-1.0.1.Final.jar — un jar complémentaire à la façade slf4j.jar (Simple Logging façade for Java) , contenant l’implémentation des logs. Ici slf4j-simple.jar(récupéré de la dernière version SFLJ) Le jar de Log4j , récupéré à partir du zip Log4j téléchargé sur le site log4j
— le jar contenant le driver base de données (configuré dans hibernate.cfg.xml). Ici mysql.jar
tutoriel-jpa-hibernate-5-lib
Classe principale et mise en œuvre persistance
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 6/8
Analysez les 6 étapes de la mise en œuvre de la sauvegarde en base d’une objet Formation Hibernate.
tutoriel-jpa-hibernate-6-lanceur
Partie 3 : Préparation Base de données
Créez dans le serveur Mysql une base de données nommée demojpa
Partie 4 : JPA en action
Exécutez la classe principale de l’aplication DemoHibernate (Click droit puis Run->run as Java Application).
Que se passe t’il ?
Partie 5 : Vérification enregistrement DB
tutoriel-jpa-hibernate-7-donnees-db
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 7/8
L’enregistrement a bien été créé. Merci JPA/Hibernate.
Partie 6 : Vérification enregistrement DB
Changez de fournisseur de persistance : utilisez EclipseLink en lieu et place de Hibernate.
Quels impacts ?
Conclusion
Dans ce tutoriel, vous avez vu JPA à l’œuvre : après avoir instancié un objet, vous avez rendu cet objet persistant en base de données, dansun enregistrement.
Correction
demohibernatejpa
EXTJS 4 : 18-20 fev
JAVASCRIPT : 18-20 fevANDROID 4 : 18-22 mar
STRUTS 2 : 4-6 marJSF : 4-6 mar
SPRING 3 : 4-8 mar
EXTJS 4 : 18-20 marSenchaTouch : 27 mar
MAVEN 3 : 27-29 marGXT 3 : 22-26 avr
31/01/13 Tutoriel JPA : votre première application JPA
www.objis.com/formation-java/tutoriel-jpa-hibernate-eclipselink-votre-premiere-application-jpa.html#intro 8/8
Objis sas au capital de 49.500 euros | OBJIS Consulting, Centre d'affaires Regus 93 rue de la Villette - 69003 Lyon | RCS Lyon SIRET48207506600027
| Organisme de formation enregistré sous le numéro 82.69.08812.69 préfecture région Rhone-Alpes. Cet enregistrement ne vaut pasagrément de l'Etat. |
Tél : 01 40 07 81 28 | Fax : 09 56 22 89 49 . Java (TM) est une marque déposée de la société SUN Microsystem.
webdesign : dadys