我在实体Visite
和Datevisite
之间存在一对多关系。
当我坚持一个新的孩子时,我在数据库中得到一个重复的行。这意味着当我持久保存新的Datevisite
时,我在数据库中找到了一个重复的行,但是有一个不同的ID。
有人能告诉我这是什么错误吗?
这是我的Visite
实体
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;
}
}
这是我的Datevisite
实体
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;
}
}
以及我如何坚持我的孩子实体
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;
}
答案 0 :(得分:0)
我认为问题出在datevisite
对象上。
在persistDatevisite()
方法中,您为其指定了Datevisite
类型的datevisite
参数。您是否有理由呼叫datevisite
的设置者并将它们设置为相同的值? (我的意思是你在做ObjectA.setFoo(ObjectA.getFoo())
。也许你应该区分你拥有的datevisite
个对象(可能会this.datevisite
?)。
看起来像这样:
this.datevisite.setDepdv(datevisite.getVisite().getDep());
或者:
datevisite.setDepdv(this.datevisite.getVisite().getDep());
取决于你想得到什么。
我也注意到你em.persist(datevisite)
。考虑使用this
就像我之前建议的那样。你也正在返回它的值(也就是说,你坚持它然后返回它,但这可能没有问题)。但是,可能是当你坚持它时,对象被保存,并且当它被返回时(这是一个疯狂的猜测)。
希望它有效,如果你告诉我们你使用的是哪种技术(编辑器,版本,.jar等),它会有所帮助。