1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
emf = Persistence.createEntityManagerFactory("pedago.jpa");
em = emf.createEntityManager();
Langue francais = em.createQuery("Select l from Langue l where code= :codeLangue", Langue.class).setParameter("codeLangue", "fr").getSingleResult();
Langue anglais = em.createQuery("Select l from Langue l where code= :codeLangue", Langue.class).setParameter("codeLangue", "en").getSingleResult();
em.getTransaction().begin();
System.out.println("id langue=" + francais.getId());
////////////////////////////////////
////début de la transaction/////////
////////////////////////////////////
// création de A
A a = new A();
em.persist(a);
// création de B
B b = new B();
em.persist(b);
// /////////////////////////////////////////////
// création de TraductionLibelle A en français//
// /////////////////////////////////////////////
TraductionTest traductionLibelleAFrancais = new TraductionTest();
traductionLibelleAFrancais.setaLibelle(a);
traductionLibelleAFrancais.setLangue(francais);
traductionLibelleAFrancais.setTexte("mon tailleur est riche");
francais.getListeTraductionTest().add(traductionLibelleAFrancais);
a.getListeTraductionTestLibelle().put(francais, traductionLibelleAFrancais);
em.persist(traductionLibelleAFrancais);
// //////////////////////////////////////////////////
// création de TraductionLibelleCourt A en français//
// //////////////////////////////////////////////////
TraductionTest traductionLibelleCourtAFrancais = new TraductionTest();
traductionLibelleCourtAFrancais.setaLibelleCourt(a);
traductionLibelleCourtAFrancais.setLangue(francais);
traductionLibelleCourtAFrancais.setTexte("mon tailleur");
francais.getListeTraductionTest().add(traductionLibelleCourtAFrancais);
a.getListeTraductionTestLibelleCourt().put(francais, traductionLibelleCourtAFrancais);
em.persist(traductionLibelleCourtAFrancais);
// ////////////////////////////////////////////
// création de TraductionLibelle A en anglais//
// ////////////////////////////////////////////
TraductionTest traductionLibelleAAnglais = new TraductionTest();
traductionLibelleAAnglais.setaLibelle(a);
traductionLibelleAAnglais.setLangue(anglais);
traductionLibelleAAnglais.setTexte("my taylor is rich");
anglais.getListeTraductionTest().add(traductionLibelleAAnglais);
a.getListeTraductionTestLibelle().put(anglais, traductionLibelleAAnglais);
em.persist(traductionLibelleAAnglais);
// ///////////////////////////////////////////////
// création de TraductionLibelleCourt en anglais//
// ///////////////////////////////////////////////
TraductionTest traductionLibelleCourtAAnglais = new TraductionTest();
traductionLibelleCourtAAnglais.setaLibelleCourt(a);
traductionLibelleCourtAAnglais.setLangue(anglais);
traductionLibelleCourtAAnglais.setTexte("my taylor");
anglais.getListeTraductionTest().add(traductionLibelleCourtAAnglais);
a.getListeTraductionTestLibelleCourt().put(anglais, traductionLibelleCourtAAnglais);
em.persist(traductionLibelleCourtAAnglais);
// /////////////////////////////////////////////
// création de TraductionLibelle B en français//
// /////////////////////////////////////////////
TraductionTest traductionLibelleBFrancais = new TraductionTest();
traductionLibelleBFrancais.setbLibelle(b);
traductionLibelleBFrancais.setLangue(francais);
traductionLibelleBFrancais.setTexte("vos fleurs sont belles");
francais.getListeTraductionTest().add(traductionLibelleBFrancais);
b.getListeTraductionTestLibelle().put(francais, traductionLibelleBFrancais);
em.persist(traductionLibelleBFrancais);
// ////////////////////////////////////////////
// création de TraductionLibelle B en anglais//
// ////////////////////////////////////////////
TraductionTest traductionLibelleBAnglais = new TraductionTest();
traductionLibelleBAnglais.setbLibelle(b);
traductionLibelleBAnglais.setLangue(anglais);
traductionLibelleBAnglais.setTexte("your flowers are beautiful");
anglais.getListeTraductionTest().add(traductionLibelleBAnglais);
b.getListeTraductionTestLibelle().put(anglais, traductionLibelleBAnglais);
em.persist(traductionLibelleBAnglais);
////////////////////////////
//fin de la transaction/////
////////////////////////////
em.getTransaction().commit();
// lecture d'un objet a
A aLu = em.createQuery("Select a from A a where id= :id", A.class).setParameter("id", a.getIdA()).getSingleResult();
for (Langue element : aLu.getListeTraductionTestLibelle().keySet()) {
System.out.println("---> Langue : " + element.getLibelle() + " traduction A Libellé : " + aLu.getListeTraductionTestLibelle().get(element).getTexte());
}
for (Langue element : aLu.getListeTraductionTestLibelleCourt().keySet()) {
System.out.println("---> Langue : " + element.getLibelle() + " traduction A LibelléCourt : " + aLu.getListeTraductionTestLibelleCourt().get(element).getTexte());
}
//lecture d'un objet B
B bLu = em.createQuery("Select b from B b where id= :id", B.class).setParameter("id", b.getIdB()).getSingleResult();
for (Langue element : bLu.getListeTraductionTestLibelle().keySet()) {
System.out.println("---> Langue : " + element.getLibelle() + " traduction B Libellé : " + bLu.getListeTraductionTestLibelle().get(element).getTexte());
}
em.close();
emf.close(); |
Partager