0

I am currently developing a Java EE application using hibernate as ORM. I am using the DAO design pattern. I want to delete a row from the contact table but I don't know why it is not working. When I delete société it works.

I have a relation between société and contact. When a contact has idSociéte=null it is deleted, but if it exist it will not delete it. When I did delete in phpmysadmin it works even if 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;
    } 

}
fbm fatma
  • 430
  • 6
  • 22
  • When you say ,,its not deleted" it means there is no error in your logs? Turn on query printing in persistence.xml and investigate the output, if you see any exveption or whether you see the actual delete sql. Is the relationship bidirectional? If yes,also post you Societe entity – yntelectual Jun 05 '17 at 13:50
  • i have no error in the log.check changes i added the société entity – fbm fatma Jun 06 '17 at 09:02
  • Do you have a foreign key in the Contacte table that reference to the table société? Setting the parent null will not delete the child in hibernate, please refer to https://stackoverflow.com/questions/26532275/hibernate-how-to-correctly-delete-children-in-onetomany – diufanman Jun 06 '17 at 09:26
  • yes i have a key in contact table which refer to société – fbm fatma Jun 06 '17 at 09:28

1 Answers1

0

when a contact has idSociéte=null it is deleted but if it exist it will not delete it.

It means if the idSociete is not null and have some value for eg. 123. You need to check if any of the contact have the same idSociete. if it's present with any other contact then your contact will not be deleted as Many contacts can be associated with same société. Try with single contact and a single société associated with it.

Pradeep Singh
  • 1,094
  • 1
  • 9
  • 24