0

I have a one-to-many relation between entity Visite and Datevisite.

I'm getting a duplicate row in the database when I persist a new child. That means when I persist a new Datevisite I find a duplicate row in the database but with a differet ID.

Can someone tell me what is the error?

here's my Visite entity

package model.entitie;

import java.io.Serializable;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
@NamedQueries({ @NamedQuery(name = "Visite.findAll", query = "select o from Visite o") })
@SequenceGenerator(name = "Visite_Id_Seq_Gen", sequenceName = "VISITE_SEQ", allocationSize = 1, initialValue = 50)
public class Visite implements Serializable {
    private static final long serialVersionUID = 2402539910369717472L;
    @Column(length = 4000)
    public String client;
    @Column(name = "INGAFF", length = 4000)
    private String ingAff;
    @Column (name="MOISVISITE",length = 4000)
    private int moisviste;
    @Column(name = "PRBVISITE" ,length = 4000)
    private String prbvisite;
    @Column (name="ETAT",length=4000)
    private String etat;
    @Column (name="ANNEE",length=4000)
    private String annee;

    public void setAnnee(String annee) {
        this.annee = annee;
    }

    public String getAnnee() {
        return annee;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public String getEtat() {
        return etat;
    }

    public void setPrbvisite(String prbvisite) {
        this.prbvisite = prbvisite;
    }

    public String getPrbvisite() {
        return prbvisite;
    }

    public void setMoisviste(int moisviste) {
        this.moisviste = moisviste;
    }

    public int getMoisviste() {
        return moisviste;
    }


    public void setIngAff(String ingAff) {
        this.ingAff = ingAff;
    }

    public String getIngAff() {
        return ingAff;
    }
    @Column(length = 4000)
    private String dep;
    @Id
    @Column(nullable = false, length = 4000)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Visite_Id_Seq_Gen")
    private String idvisite;
    @Column(length = 4000)
    private String nbvisites;
    @OneToMany(mappedBy = "visite")
    private List<Intervention> interventionList;

    @OneToMany(mappedBy = "visite",cascade =CascadeType.ALL)
    private List<Datevisite> datevisiteList;

    public Visite() {
    }

    public Visite(String client, String dep, String idvisite, String nbvisites, String ingAff) {
        this.client = client;
        this.dep = dep;
        this.idvisite = idvisite;
        this.nbvisites = nbvisites;
        this.ingAff = ingAff;
    }

    public String getClient() {
        return client;
    }

    public void setClient(String client) {

        this.client = client;
    }

    public String getDep() {
        return dep;
    }

    public void setDep(String dep) {
        this.dep = dep;
    }

    public String getIdvisite() {
        return idvisite;
    }

    public void setIdvisite(String idvisite) {
        this.idvisite = idvisite;
    }

    public String getNbvisites() {
        return nbvisites;
    }

    public void setNbvisites(String nbvisites) {
        this.nbvisites = nbvisites;
    }
    public Intervention addIntervention(Intervention intervention) {
        getInterventionList().add(intervention);
        intervention.setVisite(this);
        return intervention;
    }

    public Datevisite addDatevisite(Datevisite datevisite) {
       getDatevisiteList().add(datevisite);
        datevisite.setVisite(this);
        return datevisite;
    }

    public List<Datevisite> getDatevisiteList() {
        return datevisiteList;
    }
    public List<Intervention> getInterventionList() {
        return interventionList;
    }

    public Intervention removeIntervention(Intervention intervention) {
        getInterventionList().remove(intervention);
        intervention.setDossier(null);
        return intervention;
    }

    public Datevisite removeDatevisite(Datevisite datevisite) {
        getDatevisiteList().remove(datevisite);
        datevisite.setVisite(null);
        return datevisite;
    }

    public void setDatevisiteList(List<Datevisite> datevisiteList) {
        this.datevisiteList = datevisiteList;
    }
    public void setInterventionList(List<Intervention> interventionList) {
        this.interventionList = interventionList;
    }

}

and here's my Datevisite entity

package model.entitie;

import java.io.Serializable;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import model.entitie.Visite;

@Entity
@NamedQueries({ @NamedQuery(name = "Datevisite.findAll", query = "select o from Datevisite o") })
@SequenceGenerator(name = "DateVisite_Id_Seq_Gen", sequenceName = "DATEVISITE_SEQ", allocationSize = 1,
                   initialValue = 50)
public class Datevisite implements Serializable {
    @SuppressWarnings("compatibility:-1921858317195121496")
    private static final long serialVersionUID = -341535626783197699L;
    @Id
    @Column(nullable = false, length = 4000)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DateVisite_Id_Seq_Gen")
    private String iddatevis;

    @Column(name = "DESCRPTION")
    private String description;
    @Column(name = "CLIENTVISITE")
    private String ClientVisite;
    @Column(name = "DEPDV")
    private String depdv;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "VISITE_ID",insertable=false, updatable=false)
    private Visite visite;

    @Column(name="MOISDATEVIS")
    private String moisvisite;

    public void setMoisvisite(String moisvisite) {
        this.moisvisite = moisvisite;
    }

    public String getMoisvisite() {
        return moisvisite;
    }

    public void setAnneedatevisite(String anneedatevisite) {
        this.anneedatevisite = anneedatevisite;
    }

    public String getAnneedatevisite() {
        return anneedatevisite;
    }

    public void setNbvisited(String nbvisited) {
        this.nbvisited = nbvisited;
    }

    public String getNbvisited() {
        return nbvisited;
    }

    public void setIng(String ing) {
        this.ing = ing;
    }

    public String getIng() {
        return ing;
    }

    public void setEtatdatevisite(String etatdatevisite) {
        this.etatdatevisite = etatdatevisite;
    }

    public String getEtatdatevisite() {
        return etatdatevisite;
    }
    @Column(name="ANNEEDATEVISITE")
    private String anneedatevisite;
    @Column(name="NBVISITED")
    private String nbvisited;
    @Column(name="ING")
    private String ing;
    @Column(name="ETATDATEVISITE")
    private String etatdatevisite;

    public void setDepdv(String depdv) {
        this.depdv = depdv;
    }

    public String getDepdv() {
        return depdv;
    }

    public void setClientVisite(String ClientVisite) {
        this.ClientVisite = ClientVisite;
    }

    public String getClientVisite() {
        return ClientVisite;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public Datevisite() {
    }

    public Datevisite(String iddatevis, Visite visite, String ClientVisite, String depdv) {
        this.iddatevis = iddatevis;

        this.visite = visite;
        this.depdv = depdv;
        this.ClientVisite = ClientVisite;
    }

    public String getIddatevis() {
        return iddatevis;
    }

    public void setIddatevis(String iddatevis) {
        this.iddatevis = iddatevis;
    }

    public Visite getVisite() {
        return visite;
    }

    public void setVisite(Visite visite) {
        this.visite = visite;
    }
}

and here how i persist my child entity

public Datevisite persistDatevisite(Datevisite datevisite) {

    datevisite.setDepdv(datevisite.getVisite().getDep());
    datevisite.setClientVisite(datevisite.getVisite().getClient());
    datevisite.setDescription(datevisite.getVisite().getPrbvisite());
    datevisite.setAnneedatevisite(datevisite.getVisite().getAnnee());
    datevisite.setNbvisited(datevisite.getVisite().getNbvisites());
    datevisite.setIng(datevisite.getVisite().getIngAff());
    datevisite.setEtatdatevisite(datevisite.getVisite().getEtat());

    em.persist(datevisite);

    return datevisite;
}
pnuts
  • 58,317
  • 11
  • 87
  • 139
user3419507
  • 117
  • 1
  • 4
  • 16
  • how are you *creating* the new `Datevisite` object before you persist it? There doesn't appear to by anything in your question that looks like it should be causing data to duplicate from one call of `persistDatevisite` to the next, so it would seem that the problem is elsewhere. – Ian McLaird Sep 21 '15 at 14:34
  • No first i persist the parent (visite) than i persist the child (datavisite)..i tried even to flush entity before persisting datevisite and always the same problem – user3419507 Sep 21 '15 at 14:39
  • Please show the code that creates and populates the `Datevisite` object before it is passed to the `persistDatevisite` method. – Ian McLaird Sep 21 '15 at 14:59
  • Looking further at this, all of the fields in your child record are being overwritten with values from the parent *except* for `moisvisite`. Are you *sure* this is correct? If you populate the `moisvisite` field before calling `persistDatevisite`, are you always using a different value? If you are, you should be seeing different values in that field and the ID field. Everything else matches the parent record exactly, because you told it to copy those values. – Ian McLaird Sep 21 '15 at 15:36
  • yeah i need to copy those values in the child expect moisvisite.it'working fine expect i get a duplicate row each time i persist (i get the first record only duplicate) – user3419507 Sep 21 '15 at 15:44
  • so if you persist two *different* (created with `new`) `Datevisite` objects using this method, which each have a *different* value for `moisvisite`, you still get duplicate records? We will need a http://sscce.org/ here. We can't actually try your code as it is. – Ian McLaird Sep 21 '15 at 20:30

1 Answers1

0

I believe that the problem is with the datevisite object.

In your persistDatevisite() method you are giving it a parameter of type Datevisite called datevisite. Is there a reason for you to call setters of datevisite and set them to the same value it has? (I mean you are doing ObjectA.setFoo(ObjectA.getFoo()). Maybe you should differentiate between the datevisite objects you have (with this.datevisite maybe?).

It may look something like this:

this.datevisite.setDepdv(datevisite.getVisite().getDep());

or also:

datevisite.setDepdv(this.datevisite.getVisite().getDep());

Depending on what you want to get.

I also noticed that you do em.persist(datevisite). Consider using this on that just like I suggested before. You are also returning its value (that is, you are persisting it and then returning it, but that may be no problem). However, it may be that when you persist it the object is saved and also when it is returned (a wild guess this one).

Hope it works, and it would help if you told us what technology are you using (editor, version, .jar's etc).

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • 1
    thank you for help... i think i started to understand the problem..when i persist a new datevisite.it persist all query in the cache... i user em.clear but the same result always – user3419507 Sep 21 '15 at 14:54
  • That's not what he's doing. He's calling setters to populate fields that take their value from the *parent* record (`Visite`). That's a little unorthodox, but it's not really wrong. And there's nothing to suggest that the method parameter is a property of the object containing the `persistDatevisite` method, so the `this` option would probably result in a compile error. – Ian McLaird Sep 21 '15 at 15:06