Salut,
je suis entrain de réaliser une application sous forme d'un répertoire téléphonique, ou on sauvegarde des contact (numéro de téléphone(mobile,fixe ou fax), adresse, mail +des informations supplémentaires) en donnant la possibilité de classer les contacts dans des groupes.
j'ai les classes : Contact, EntrepriseContact (qui hérite de la classe Contact), PhoneNumber, Group et Address.
Contact.java
import java.util.ArrayList; import java.util.List; import javax.persistence.*; @Entity @Table(name = "contact") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "Type")@DiscriminatorValue("contact") @SuppressWarnings("SerializableClass") public class Contact { @Id @Column(name = "id") protected int id; protected String firstName; protected String lastName; protected String email; @Embedded protected Address rAddress; @OneToMany(mappedBy = "rContact" , cascade = CascadeType.ALL) protected List<PhoneNumber> rPhoneNumber; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "contact_groupe",joinColumns=@JoinColumn(name = "id_contact"), inverseJoinColumns= @JoinColumn(name = "id_group")) protected List<Group> rGroup; public Contact() { rPhoneNumber = new ArrayList<>(); rGroup = new ArrayList<>(); } public Contact(int id, String firstName, String lastName, String email) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.email = email; rPhoneNumber = new ArrayList<>(); rGroup = new ArrayList<>(); } public String getName(){ return firstName + " " + lastName.toUpperCase(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } // ------------- Address ------------------ public Address getrAddress() { return rAddress; } public void setrAddress(Address rAddress) { this.rAddress = rAddress; } public void addAddress(Address a){ setrAddress(a); } // ------------- PhoneNumber ------------------ public List<PhoneNumber> getrPhoneNumber() { return rPhoneNumber; } public void setrPhoneNumber(ArrayList<PhoneNumber> rPhoneNumber) { this.rPhoneNumber = rPhoneNumber; } public void addPhoneNumber(PhoneNumber p){ if( !rPhoneNumber.contains(p) ){ if (p.getrContact() != null) p.getrContact().getrPhoneNumber().remove(p); p.setrContact(this); rPhoneNumber.add(p); } } // ------------- Group ------------------ public List<Group> getrGroup() { return rGroup; } public void setrGroup(ArrayList<Group> rGroup) { this.rGroup = rGroup; } public void addGroup(Group g){ if( !rGroup.contains(g) ){ g.getrContact().add(this); rGroup.add(g); } } @Override public boolean equals(Object obj) { return this.id == ((Contact) obj).id; } @Override public int hashCode() { int hash = 5; hash = 29 * hash + this.id; return hash; } @Override public String toString() { return getName(); } }
Address.java
import javax.persistence.*; @Embeddable @SuppressWarnings("SerializableClass") public class Address { @Transient private int id; private String street; private String city; private String zipCode; private String country; public Address() { } public Address(int id, String street, String city, String zipCode, String country) { this.id = id; this.street = street; this.city = city; this.zipCode = zipCode; this.country = country; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public boolean equals(Object obj) { if(obj instanceof Address) return this.id == ((Address) obj).id; else return super.equals(obj); } @Override public int hashCode() { int hash = 5; hash = 79 * hash + this.id; return hash; } @Override public String toString() { return street + ", " + city + " " + zipCode + ", " + country; } }
EntrepriseContact.java
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import javax.persistence.*; @Entity @DiscriminatorValue("entreprise_contact") @SuppressWarnings("SerializableClass") public class EntrepriseContact extends Contact { public static final String ENTREPRISE_KIND_SARL = "SARL"; public static final String ENTREPRISE_KIND_EURL = "EURL"; public static final String ENTREPRISE_KIND_SNC = "SNC"; public static final String ENTREPRISE_KIND_SCS = "SCS" ; public static final String ENTREPRISE_KIND_SCA = "SCA" ; private String SIRET; private String denomination; private String entrepriseKind; @Temporal(javax.persistence.TemporalType.DATE) private Date creationDate; private String activity; private double shareCapital; public EntrepriseContact() { } public EntrepriseContact(int id, String firstName, String lastName, String email, String SIRET, String denomination, String entrepriseKind, Date creationDate, String activity, double shareCapital ) { super(id, firstName, lastName, email); this.SIRET = SIRET; this.denomination = denomination; this.entrepriseKind = entrepriseKind; this.creationDate = creationDate; this.activity = activity; this.shareCapital = shareCapital; } @Override public String getName() { return denomination + " " + entrepriseKind + " (" + super.getName() + ")"; //To change body of generated methods, choose Tools | Templates. } public String getSIRET() { return SIRET; } public void setSIRET(String SIRET) { this.SIRET = SIRET; } public String getDenomination() { return denomination; } public void setDenomination(String denomination) { this.denomination = denomination; } public String getEntrepriseKind() { return entrepriseKind; } public void setEntrepriseKind(String entrepriseKind) { this.entrepriseKind = entrepriseKind; } public Date getCreationDate() { return creationDate; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public String getActivity() { return activity; } public void setActivity(String activity) { this.activity = activity; } public double getShareCapital() { return shareCapital; } public void setShareCapital(double shareCapital) { this.shareCapital = shareCapital; } @Override public boolean equals(Object obj) { return this.id == ((Contact) obj).id; } @Override public String toString() { // Set your date format SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH); // Get Date String according to date format (12 March 1970) String formatedDate = sdf.format(creationDate); return getName() + ", since " + formatedDate; } }
PhoneNumber.java
import javax.persistence.*; @Entity @Table(name = "phone_number") @SuppressWarnings("SerializableClass") public class PhoneNumber { @Id @Column(name = "id") private int id; private String phoneKind; private String number; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "id_contact") private Contact rContact; public PhoneNumber() { } public static final String PHONE_FIX = "FIX"; public static final String PHONE_MOBILE = "MOBILE"; public static final String PHONE_FAX = "FAX"; public PhoneNumber(int id, String phoneKind, String number) { this.id = id; this.phoneKind = phoneKind; this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPhoneKind() { return phoneKind; } public void setPhoneKind(String phoneKind) { this.phoneKind = phoneKind; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } // ------------- Contact ------------------ public Contact getrContact() { return rContact; } public void setrContact(Contact rContact) { this.rContact = rContact; } public void addContact(Contact c){ if(!c.getrPhoneNumber().contains(this)){ if (rContact != null) rContact.getrPhoneNumber().remove(this); this.setrContact(c); c.getrPhoneNumber().add(this); } } @Override @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") public boolean equals(Object obj) { return this.id == ((PhoneNumber) obj).id; } @Override public int hashCode() { int hash = 3; hash = 97 * hash + this.id; return hash; } @Override public String toString() { // Ex: (MOBILE) 0661123456 return "(" + phoneKind + ") " + number; } }
Group.java
import java.util.ArrayList; import java.util.List; import javax.persistence.*; @Entity @Table(name = "groupe") @SuppressWarnings({"SerializableClass", "ValidPrimaryTableName"}) public class Group { @Id @Column(name = "id") private int id; private String groupName; @ManyToMany(mappedBy = "rGroup",cascade = CascadeType.ALL) private List<Contact> rContact; public Group() { rContact = new ArrayList<>(); } public Group(int id, String groupName) { this.id = id; this.groupName = groupName; rContact = new ArrayList<>(); } public double getGroupShareCapital(){ double groupShareCapital = 0; for (Contact c : rContact) { if(c instanceof EntrepriseContact) groupShareCapital += ((EntrepriseContact)c).getShareCapital(); } return groupShareCapital; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } // ------------- Contact ------------------ public List<Contact> getrContact() { return rContact; } public void setrContact(List<Contact> rContact) { this.rContact = rContact; } public void addContact(Contact c){ if( !rContact.contains(c) ){ c.getrGroup().add(this); rContact.add(c); } } @Override public boolean equals(Object obj) { return this.id == ((Group) obj).id; } @Override public int hashCode() { int hash = 5; hash = 97 * hash + this.id; return hash; } }
Voici aussi le diagramme de classe et le modèle de domaine :
le fichier de persistance xml est le suivant :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" 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 https://www.oracle.com/java/technologies/ <persistence-unit name="carnet-unit"> <class>bda.tp5.jpa.Group</class> <class>bda.tp5.jpa.PhoneNumber</class> <class>bda.tp5.jpa.Contact</class> <class>bda.tp5.jpa.Address</class> <class>bda.tp5.jpa.EntrepriseContact</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost/bda_tp5_carnet"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="create"/> </properties> </persistence-unit> </persistence>
Afin de tester le truc on essay de persister trois contact(ali,karim et Farouk) avec leurs informations...
import java.util.Date; import javax.persistence.*; public static void main(String[] args) { //Creer la connection : EntityManagerFactory emf = Persistence.createEntityManagerFactory("carnet-unit"); EntityManager em = emf.createEntityManager(); //creer une transaction : EntityTransaction tx = em.getTransaction(); Contact ali = new Contact(1, "Ali", "Benmostafa", "ali.benmostafa@gmail.com"); Contact sntf = new EntrepriseContact(2, "Farouk", "Souleymen", "farouk.souleymen@sntf.dz", "01234567890123", "SNTF", EntrepriseContact.ENTREPRISE_KIND_SNC, new Date(76,2,26), "Transport ferroviaire", 2070100.00); Contact karim = new Contact(3, "Karim", "Boualam", "karim.boualam@hotmail.com"); // -------------------------------------------------------------- ali.addAddress(new Address(1, "87, Ali Mendjeli", "Constantine", "25000", "Algeria")); sntf.addAddress(new Address(1, "21,23 Boulevard Mohamed V", "Alger", "16000", "Algeria")); // -------------------------------------------------------------- ali.addPhoneNumber(new PhoneNumber(1, PhoneNumber.PHONE_FIX, "031123456")); ali.addPhoneNumber(new PhoneNumber(2, PhoneNumber.PHONE_MOBILE, "0770123456")); sntf.addPhoneNumber(new PhoneNumber(3, PhoneNumber.PHONE_FIX, "021711510")); sntf.addPhoneNumber(new PhoneNumber(4, PhoneNumber.PHONE_FAX, "021633934")); karim.addPhoneNumber(new PhoneNumber(5, PhoneNumber.PHONE_MOBILE, "0661123456")); Group groupFootball = new Group(1, "CSC"); groupFootball.addContact(ali); groupFootball.addContact(karim); Group groupJob = new Group(1, "Agence"); ali.addGroup(groupJob); groupJob.addContact(sntf); tx.begin(); em.persist(ali); em.persist(karim); em.persist(sntf); tx.commit(); em.close(); emf.close(); }
Mais le problème c'est que dans ma base de donnée je ne voix que Ali et Karim !!
La table entreprise_contact est toujours vide :(
Que dois-je faire??
PS: aucune exception n'est lancée, le programme s'execute sans aucune erreur -_-
Afficher la suite
18 déc. 2016 à 21:42
C'est mis pour pouvoir gérer les contacts d'un groupe, ça traduit la relation bidirectionnelle entre la classe Group et Contact
On a valider tous ce qui est java dans les classes Contact.java, Address.java, PhoneNumber.java, Group.java et EntrepriseContact.java...
Donc le problème reste dans les annotations @ et la classe main.java.