2017-06-05 139 views
0

我目前正在开发一个使用hibernate作为ORM的Java EE应用程序。 我正在使用DAO设计模式。我想从联系人表中删除一行,但我不知道它为什么不起作用。当我删除société它的作品。休眠不删除行

我有一个sociétécontact之间的关系。当联系人有idSociéte=null时,它将被删除,但如果存在,则不会删除它。当我删除phpmysadmin它即使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

当你说,,它没有被删除“这意味着你的日志没有错误?打开查询打印在persistence.xml中,并调查输出,如果你看到任何exveption或是否看到实际的删除sql。双方的关系?如果是的话,也张贴你Societe实体 – yntelectual

+0

我没有错误在log.check更改我添加了société实体 –

+0

你有一个外键在Contacte表引用表société?设置parent null不会删除hibernate中的孩子,请参考https://stackoverflow.com/questions/26532275/hibernate-how-to-correctly-delete-children-in-onetomany – diufanman

回答

0

当联系人有idSociéte= null,则删除,但如果它存在,它 不会删除它。

这意味着,如果idSociete不为空,并有一些值例如。 123.你需要检查任何联系人是否有相同的idSociete。如果它与任何其他联系人一起出现,则您的联系人不会被删除,因为许多联系人可以与同一社交关联。尝试与单一联系人和与之相关的单个社交活动。