2017-06-05 4 views
0

Ich entwickle gerade eine Java EE-Anwendung mit Hibernate als ORM. Ich verwende das DAO Designmuster. Ich möchte eine Zeile aus der Kontakttabelle löschen, aber ich weiß nicht, warum sie nicht funktioniert. Wenn ich société lösche, funktioniert es.Hibernate löscht keine Zeilen

Ich habe eine Beziehung zwischen société und contact. Wenn ein Kontakt idSociéte=null hat, wird er gelöscht, aber wenn er existiert, wird er nicht gelöscht. Wenn ich löschte in phpmysadmin funktioniert es auch wenn idSociété not null.

@Transactional(readOnly = false) 
public class GenericDaoImp<T> implements GenericDao<T> { 
    @PersistenceContext 
    private EntityManager em; 

    protected Class<T> daoType; 

    public GenericDaoImp() { 
     Type t = getClass().getGenericSuperclass(); 
     ParameterizedType pt = (ParameterizedType) t; 
     daoType = (Class) pt.getActualTypeArguments()[0]; 
    } 

    public EntityManager getEm() { 
     return em; 
    } 

    public void setEm(EntityManager em) { 
     this.em = em; 
    } 

    public Class<T> getDaoType() { 
     return daoType; 
    } 

    public void setDaoType(Class<T> daoType) { 
     this.daoType = daoType; 
    } 

    public void insert(T t) { 
     // TODO Auto-generated method stub 
     em.persist(t); 

    } 

    public void update(T t) { 
     // TODO Auto-generated method stub 
     em.merge(t); 
    } 

    public void delete(T t) { 
     // TODO Auto-generated method stub 
     Object managed = em.merge(t); 
     em.remove(managed); 


    } 

    public T findById(Class<T> t, int id) { 
     // TODO Auto-generated method stub 
     return em.find(daoType, id); 
    } 

    public List<T> findAll() { 
     // TODO Auto-generated method stub 
     Query query = em.createQuery("SELECT e FROM " + daoType.getName() + " e"); 
     return (List<T>) query.getResultList(); 
    } 

} 



    package biz.picosoft.entity; 


    @Entity 
    @Table(name = "Contacte") 
    public class Contacte implements Serializable { 

     private static final long serialVersionUID = 1L; 
     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY) 
     @Column(name = "idContact") 
     int idContact; 
     @Column(name = "nom") 
     String nom; 
     @Column(name = "mail") 
     String mail; 
     @Column(name = "téléphone") 
     String téléphone; 
     @Column(name = "adresse") 
     String adresse; 
     @ManyToOne 
     @JoinColumn(name = "société_id") 
     private Société société; 

     public Contacte() { 
      super(); 
     } 

     public long getIdContact() { 
      return idContact; 
     } 

     public Contacte(String nom, String mail, String téléphone, String adresse, Société société) { 
      super(); 
      this.nom = nom; 
      this.mail = mail; 
      this.téléphone = téléphone; 
      this.adresse = adresse; 
      this.société = société; 
     } 

     public Contacte(int idContact, String nom, String mail, String téléphone, String adresse, Société société) { 
      super(); 
      this.idContact = idContact; 
      this.nom = nom; 
      this.mail = mail; 
      this.téléphone = téléphone; 
      this.adresse = adresse; 
      this.société = société; 
     } 

     public void setIdContact(int idContact) { 
      this.idContact = idContact; 
     } 

     public String getNom() { 
      return nom; 
     } 

     public void setNom(String nom) { 
      this.nom = nom; 
     } 

     public String getMail() { 
      return mail; 
     } 

     public void setMail(String mail) { 
      this.mail = mail; 
     } 

     public String getTéléphone() { 
      return téléphone; 
     } 

     public void setTéléphone(String téléphone) { 
      this.téléphone = téléphone; 
     } 

     public String getAdresse() { 
      return adresse; 
     } 

     public void setAdresse(String adresse) { 
      this.adresse = adresse; 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + (int) (idContact^(idContact >>> 32)); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      Contacte other = (Contacte) obj; 
      if (idContact != other.idContact) 
       return false; 
      return true; 
     } 

     public Société getSociété() { 
      return société; 
     } 

     public void setSociété(Société société) { 
      this.société = société; 
     } 

    } 

    package biz.picosoft.daoImpl; 


    @Component 
    public class ContacteDaoImpl extends GenericDaoImp<Contacte> implements ContacteDao { 

    } 

package biz.picosoft.entity; 

import java.io.Serializable; 
import java.util.List; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

@Entity(name = "société") 
@Table(name="société") 
public class Société implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "idSociété") 
    int idSociété; 
    @Column(name = "nom") 
    String nom; 
    @Column(name = "email") 
    String email; 
    @Column(name = "télèphone") 
    String télèphone; 
    @Column(name = "adress") 
    String adress; 
    @OneToMany (fetch = FetchType.EAGER,mappedBy = "société", cascade = CascadeType.ALL) 
    private List<Contacte> contacts; 

    public Société(String nom, String email, String télèphone, String adress) { 
     super(); 
     this.nom = nom; 
     this.email = email; 
     this.télèphone = télèphone; 
     this.adress = adress; 
    } 



    public Société(int idSociété, String nom, String email, String télèphone, String adress) { 
     super(); 
     this.idSociété = idSociété; 
     this.nom = nom; 
     this.email = email; 
     this.télèphone = télèphone; 
     this.adress = adress; 
     this.contacts = contacts; 
    } 



    public Société() { 
     super(); 
    } 



    public int getIdSociété() { 
     return idSociété; 
    } 

    public void setIdSociété(int idSociété) { 
     this.idSociété = idSociété; 
    } 
    @Column(name = "nom") 
    public String getNom() { 
     return nom; 
    } 

    public void setNom(String nom) { 
     this.nom = nom; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getTélèphone() { 
     return télèphone; 
    } 

    public void setTélèphone(String télèphone) { 
     this.télèphone = télèphone; 
    } 

    public String getAdress() { 
     return adress; 
    } 

    public void setAdress(String adress) { 
     this.adress = adress; 
    } 

    public List<Contacte> getContacts() { 
     return contacts; 
    } 



    public void setContacts(List<Contacte> contacts) { 
     this.contacts = contacts; 
    } 



    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + idSociété; 
     return result; 
    } 



    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Société other = (Société) obj; 
     if (idSociété != other.idSociété) 
      return false; 
     return true; 
    } 

} 
+0

Wenn Sie sagen ,, es ist nicht gelöscht "bedeutet es, dass es keinen Fehler in Ihren Protokollen gibt Aktivieren Sie den Abfragedruck in der Datei persistence.xml und untersuchen Sie die Ausgabe, wenn Sie eine Exoption sehen oder die tatsächliche Lösch-SQL sehen die Beziehung bidirektionale? Wenn ja, auch post Sie Societe entity – yntelectual

+0

Ich habe keinen Fehler in der log.check Änderungen Ich habe die société-Entität –

+0

Haben Sie einen Fremdschlüssel in der Contacte-Tabelle, die auf die Tabelle société setzen? wird das Kind im Ruhezustand nicht löschen, bitte beziehen Sie sich auf https://stackoverflow.com/questions/26532275/hibernate-how-to-correctly-delete-children-in-onetomany – diufanman

Antwort

0

, wenn ein Kontakt hat idSociéte = null gelöscht wird, aber wenn es es existieren wird es nicht gelöscht werden.

Es bedeutet, wenn die IDSociete nicht null ist und einen Wert für zB haben. 123. Sie müssen überprüfen, ob einer der Kontakte dieselbe idSociete hat. Wenn es bei einem anderen Kontakt vorhanden ist, wird Ihr Kontakt nicht gelöscht, da viele Kontakte mit demselben Kontakt verknüpft werden können. Versuchen Sie es mit einem einzelnen Kontakt und einer einzelnen damit verbundenen Gesellschaft.

Verwandte Themen