2017-04-10 15 views
0

基本的に私はClient(従業員)、Personalinfo、Otherinfoなどのテーブルを持つようにPDS(Personal Data Sheet)を実装しています。Java Persistence:子行を追加または更新できません。外部キー制約が失敗します。

クライアントテーブル構造:

idClients, name, institute, username, password, usertype 

Personalinfoテーブル構造:

idPersonalInfo, agencyno, bloodtype, status, citizenship, address, idClients 

私は正常にクライアントを追加することができますが、私は/更新personalinfoを追加しようとしているとき、私は

MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails 
を取得

personalinfoクラスには次の行もあります:

クライアントクラスで
@JoinColumn(name = "idClients", referencedColumnName = "idClients", insertable = false, updatable = false) 

は私も含ま:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "clients")  
    private Personalinfo personalinfo; 

しかし、私は制約違反を取得しています理由を把握することはできません。

Error Code: 1452 
Call: INSERT INTO personalinfo (...) VALUES (...) 
    bind => [7parameters bound] 
Query: InsertObjectQuery(com.profiling.database.Personalinfo[ idPersonalInfo=null ]) 

EDIT:

if (personal != null) { 
      ..... 
      personal.setClients(client); 
      personal.setIdPersonalInfo(client.getIdClients()); 

      EntityManager em = emanager.getEntityManager(); 
      em.getTransaction().begin(); 
      em.persist(personal); 
      em.getTransaction().commit(); 
      JOptionPane.showMessageDialog(rootPane, "Personal Info Updated Successfully"); 
     } else { 
      personal = new Personalinfo(); 
      .... 
      personal.setClients(client); 
      client.setPersonalinfo(personal); 
      EntityManager em = emanager.getEntityManager(); 

      em.getTransaction().begin(); 
      em.persist(personal); 
      em.getTransaction().commit(); 
      JOptionPane.showMessageDialog(rootPane, "New Personal Info Save"); 
     } 

EDIT クライアントクラス:

@Entity 
@Table(name = "clients") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Clients.findAll", query = "SELECT c FROM Clients c"), 
    @NamedQuery(name = "Clients.findByIdClients", query = "SELECT c FROM Clients c WHERE c.idClients = :idClients"), 
    @NamedQuery(name = "Clients.findByFirstName", query = "SELECT c FROM Clients c WHERE c.firstName = :firstName"), 
    @NamedQuery(name = "Clients.findByGender", query = "SELECT c FROM Clients c WHERE c.gender = :gender"), 
    @NamedQuery(name = "Clients.findByLastName", query = "SELECT c FROM Clients c WHERE c.lastName = :lastName"), 
    @NamedQuery(name = "Clients.findByMiddlename", query = "SELECT c FROM Clients c WHERE c.middlename = :middlename"), 
    @NamedQuery(name = "Clients.findByNameExtension", query = "SELECT c FROM Clients c WHERE c.nameExtension = :nameExtension")}) 
public class Clients implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "idClients") 
    private Integer idClients; 
    @Column(name = "FirstName") 
    private String firstName; 
    @Column(name = "Gender") 
    private String gender; 
    @Column(name = "LastName") 
    private String lastName; 
    @Column(name = "Middlename") 
    private String middlename; 
    @Column(name = "NameExtension") 
    private String nameExtension; 
    @Column(name = "Institute") 
    private String institute; 

    //value added 
    @Column(name = "username") 
    private String username; 
    @Column(name = "password") 
    private String password; 
    @Column(name = "userType") 
    private String userType; 


    @OneToMany(mappedBy = "idClients") 
    private List<Workexpe> workexpeList; 

    @OneToMany(mappedBy = "idClients") 
    private List<Ratingscore> ratingscoreList; 

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "clients") 
    private Familyback familyback; 

    @OneToMany(mappedBy = "idClients") 
    private List<Civilservice> civilserviceList; 

    @OneToMany(mappedBy = "idClients") 
    private List<Staffscore> staffscoreList; 

    @OneToMany(mappedBy = "idClients") 
    private List<Referenceinfo> referenceinfoList; 

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "clients")  
    private Personalinfo personalinfo; 

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "clients") 
// @OneToOne(mappedBy = "idClients") 
    private Otherinfo otherinfo; 

    @OneToMany(mappedBy = "idClients") 
    private List<Trainingprog> trainingprogList; 
    @OneToMany(mappedBy = "idClients") 
    private List<Children> childrenList; 
    @OneToMany(mappedBy = "idClients") 
    private List<Specialskills> specialskillsList; 
    @OneToMany(mappedBy = "idClients") 
    private List<Educback> educbackList; 
    @OneToMany(mappedBy = "idClients") 
    private List<Voluntarywork> voluntaryworkList; 

    public Clients() { 
    } 

    public Clients(Integer idClients) { 
     this.idClients = idClients; 
    } 

    public Integer getIdClients() { 
     return idClients; 
    } 

    public void setIdClients(Integer idClients) { 
     this.idClients = idClients; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getMiddlename() { 
     return middlename; 
    } 

    public void setMiddlename(String middlename) { 
     this.middlename = middlename; 
    } 

    public String getNameExtension() { 
     return nameExtension; 
    } 

    public void setNameExtension(String nameExtension) { 
     this.nameExtension = nameExtension; 
    } 

    @XmlTransient 
    public List<Workexpe> getWorkexpeList() { 
     return workexpeList; 
    } 

    public void setWorkexpeList(List<Workexpe> workexpeList) { 
     this.workexpeList = workexpeList; 
    } 

    @XmlTransient 
    public List<Ratingscore> getRatingscoreList() { 
     return ratingscoreList; 
    } 

    public void setRatingscoreList(List<Ratingscore> ratingscoreList) { 
     this.ratingscoreList = ratingscoreList; 
    } 

    public Familyback getFamilyback() { 
     return familyback; 
    } 

    public void setFamilyback(Familyback familyback) { 
     this.familyback = familyback; 
    } 

    @XmlTransient 
    public List<Civilservice> getCivilserviceList() { 
     return civilserviceList; 
    } 

    public void setCivilserviceList(List<Civilservice> civilserviceList) { 
     this.civilserviceList = civilserviceList; 
    } 

    @XmlTransient 
    public List<Staffscore> getStaffscoreList() { 
     return staffscoreList; 
    } 

    public void setStaffscoreList(List<Staffscore> staffscoreList) { 
     this.staffscoreList = staffscoreList; 
    } 

    @XmlTransient 
    public List<Referenceinfo> getReferenceinfoList() { 
     return referenceinfoList; 
    } 

    public void setReferenceinfoList(List<Referenceinfo> referenceinfoList) { 
     this.referenceinfoList = referenceinfoList; 
    } 

    public Personalinfo getPersonalinfo() { 
     return personalinfo; 
    } 

    public void setPersonalinfo(Personalinfo personalinfo) { 
     this.personalinfo = personalinfo; 
    } 

    public Otherinfo getOtherinfo() { 
     return otherinfo; 
    } 

    public void setOtherinfo(Otherinfo otherinfo) { 
     this.otherinfo = otherinfo; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getUserType() { 
     return userType; 
    } 

    public void setUserType(String userType) { 
     this.userType = userType; 
    } 



    @XmlTransient 
    public List<Trainingprog> getTrainingprogList() { 
     return trainingprogList; 
    } 

    public void setTrainingprogList(List<Trainingprog> trainingprogList) { 
     this.trainingprogList = trainingprogList; 
    } 

    @XmlTransient 
    public List<Children> getChildrenList() { 
     return childrenList; 
    } 

    public void setChildrenList(List<Children> childrenList) { 
     this.childrenList = childrenList; 
    } 

    @XmlTransient 
    public List<Specialskills> getSpecialskillsList() { 
     return specialskillsList; 
    } 

    public void setSpecialskillsList(List<Specialskills> specialskillsList) { 
     this.specialskillsList = specialskillsList; 
    } 

    @XmlTransient 
    public List<Educback> getEducbackList() { 
     return educbackList; 
    } 

    public void setEducbackList(List<Educback> educbackList) { 
     this.educbackList = educbackList; 
    } 

    @XmlTransient 
    public List<Voluntarywork> getVoluntaryworkList() { 
     return voluntaryworkList; 
    } 

    public void setVoluntaryworkList(List<Voluntarywork> voluntaryworkList) { 
     this.voluntaryworkList = voluntaryworkList; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (idClients != null ? idClients.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Clients)) { 
      return false; 
     } 
     Clients other = (Clients) object; 
     if ((this.idClients == null && other.idClients != null) || (this.idClients != null && !this.idClients.equals(other.idClients))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return this.lastName + ", " + this.firstName; 
    } 

    public String getInstitute() { 
     return institute; 
    } 

    public void setInstitute(String institute) { 
     this.institute = institute; 
    } 

} 

Personalinfo

@Entity 
@Table(name = "personalinfo") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Personalinfo.findAll", query = "SELECT p FROM Personalinfo p"), 
    @NamedQuery(name = "Personalinfo.findByIdPersonalInfo", query = "SELECT p FROM Personalinfo p WHERE p.idPersonalInfo = :idPersonalInfo"), 
    @NamedQuery(name = "Personalinfo.findByAgencyNumber", query = "SELECT p FROM Personalinfo p WHERE p.agencyNumber = :agencyNumber"), 
    @NamedQuery(name = "Personalinfo.findByBloodType", query = "SELECT p FROM Personalinfo p WHERE p.bloodType = :bloodType"), 
    @NamedQuery(name = "Personalinfo.findByCellphone", query = "SELECT p FROM Personalinfo p WHERE p.cellphone = :cellphone"), 
    @NamedQuery(name = "Personalinfo.findByCevilStatus", query = "SELECT p FROM Personalinfo p WHERE p.cevilStatus = :cevilStatus"), 
    @NamedQuery(name = "Personalinfo.findByCitizenship", query = "SELECT p FROM Personalinfo p WHERE p.citizenship = :citizenship"), 
    @NamedQuery(name = "Personalinfo.findByDateofBirth", query = "SELECT p FROM Personalinfo p WHERE p.dateofBirth = :dateofBirth"), 
    @NamedQuery(name = "Personalinfo.findByEmailAdd", query = "SELECT p FROM Personalinfo p WHERE p.emailAdd = :emailAdd"), 
    @NamedQuery(name = "Personalinfo.findByGsis", query = "SELECT p FROM Personalinfo p WHERE p.gsis = :gsis"), 
    @NamedQuery(name = "Personalinfo.findByHeight", query = "SELECT p FROM Personalinfo p WHERE p.height = :height"), 
    @NamedQuery(name = "Personalinfo.findByPagibig", query = "SELECT p FROM Personalinfo p WHERE p.pagibig = :pagibig"), 
    @NamedQuery(name = "Personalinfo.findByPermanentAddress", query = "SELECT p FROM Personalinfo p WHERE p.permanentAddress = :permanentAddress"), 
    @NamedQuery(name = "Personalinfo.findByPhilHealth", query = "SELECT p FROM Personalinfo p WHERE p.philHealth = :philHealth"), 
    @NamedQuery(name = "Personalinfo.findByPlaceofBirth", query = "SELECT p FROM Personalinfo p WHERE p.placeofBirth = :placeofBirth"), 
    @NamedQuery(name = "Personalinfo.findByResidentialAddress", query = "SELECT p FROM Personalinfo p WHERE p.residentialAddress = :residentialAddress"), 
    @NamedQuery(name = "Personalinfo.findBySssNo", query = "SELECT p FROM Personalinfo p WHERE p.sssNo = :sssNo"), 
    @NamedQuery(name = "Personalinfo.findByTelephone1", query = "SELECT p FROM Personalinfo p WHERE p.telephone1 = :telephone1"), 
    @NamedQuery(name = "Personalinfo.findByTelephone2", query = "SELECT p FROM Personalinfo p WHERE p.telephone2 = :telephone2"), 
    @NamedQuery(name = "Personalinfo.findByTin", query = "SELECT p FROM Personalinfo p WHERE p.tin = :tin"), 
    @NamedQuery(name = "Personalinfo.findByWeight", query = "SELECT p FROM Personalinfo p WHERE p.weight = :weight"), 
    @NamedQuery(name = "Personalinfo.findByZipcode1", query = "SELECT p FROM Personalinfo p WHERE p.zipcode1 = :zipcode1"), 
    @NamedQuery(name = "Personalinfo.findByZipcode2", query = "SELECT p FROM Personalinfo p WHERE p.zipcode2 = :zipcode2")}) 
public class Personalinfo implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "idPersonalInfo") 
    private Integer idPersonalInfo; 
    @Column(name = "AgencyNumber") 
    private String agencyNumber; 
    @Column(name = "BloodType") 
    private String bloodType; 
    @Column(name = "Cellphone") 
    private String cellphone; 
    @Column(name = "CevilStatus") 
    private String cevilStatus; 
    @Column(name = "Citizenship") 
    private String citizenship; 
    @Column(name = "DateofBirth") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateofBirth; 
    @Column(name = "EmailAdd") 
    private String emailAdd; 
    @Column(name = "GSIS") 
    private String gsis; 
    @Column(name = "Height") 
    private String height; 
    @Column(name = "Pagibig") 
    private String pagibig; 
    @Column(name = "PermanentAddress") 
    private String permanentAddress; 
    @Column(name = "PhilHealth") 
    private String philHealth; 
    @Lob 
    @Column(name = "Picture") 
    private byte[] picture; 
    @Column(name = "PlaceofBirth") 
    private String placeofBirth; 
    @Column(name = "ResidentialAddress") 
    private String residentialAddress; 
    @Column(name = "SssNo") 
    private String sssNo; 
    @Column(name = "Telephone1") 
    private String telephone1; 
    @Column(name = "Telephone2") 
    private String telephone2; 
    @Column(name = "Tin") 
    private String tin; 
    @Column(name = "Weight") 
    private String weight; 
    @Column(name = "Zipcode1") 
    private String zipcode1; 
    @Column(name = "Zipcode2") 
    private String zipcode2; 
    @Column(name = "IdClients") 
    private String idClients; 

    @JoinColumn(name = "idClients", referencedColumnName = "idClients", insertable = false, updatable = false) 
// @JoinColumn(name = "idClients", referencedColumnName = "idClients") 
    @OneToOne(optional = false) 
    private Clients clients; 

    public Personalinfo() { 
    } 

    public Personalinfo(Integer idPersonalInfo) { 
     this.idPersonalInfo = idPersonalInfo; 
    } 

    public Integer getIdPersonalInfo() { 
     return idPersonalInfo; 
    } 

    public void setIdPersonalInfo(Integer idPersonalInfo) { 
     this.idPersonalInfo = idPersonalInfo; 
    } 

    public String getAgencyNumber() { 
     return agencyNumber; 
    } 

    public void setAgencyNumber(String agencyNumber) { 
     this.agencyNumber = agencyNumber; 
    } 

    public String getBloodType() { 
     return bloodType; 
    } 

    public void setBloodType(String bloodType) { 
     this.bloodType = bloodType; 
    } 

    public String getCellphone() { 
     return cellphone; 
    } 

    public void setCellphone(String cellphone) { 
     this.cellphone = cellphone; 
    } 

    public String getCevilStatus() { 
     return cevilStatus; 
    } 

    public void setCevilStatus(String cevilStatus) { 
     this.cevilStatus = cevilStatus; 
    } 

    public String getCitizenship() { 
     return citizenship; 
    } 

    public void setCitizenship(String citizenship) { 
     this.citizenship = citizenship; 
    } 

    public Date getDateofBirth() { 
     return dateofBirth; 
    } 

    public void setDateofBirth(Date dateofBirth) { 
     this.dateofBirth = dateofBirth; 
    } 

    public String getEmailAdd() { 
     return emailAdd; 
    } 

    public void setEmailAdd(String emailAdd) { 
     this.emailAdd = emailAdd; 
    } 

    public String getGsis() { 
     return gsis; 
    } 

    public void setGsis(String gsis) { 
     this.gsis = gsis; 
    } 

    public String getHeight() { 
     return height; 
    } 

    public void setHeight(String height) { 
     this.height = height; 
    } 

    public String getPagibig() { 
     return pagibig; 
    } 

    public void setPagibig(String pagibig) { 
     this.pagibig = pagibig; 
    } 

    public String getPermanentAddress() { 
     return permanentAddress; 
    } 

    public void setPermanentAddress(String permanentAddress) { 
     this.permanentAddress = permanentAddress; 
    } 

    public String getPhilHealth() { 
     return philHealth; 
    } 

    public void setPhilHealth(String philHealth) { 
     this.philHealth = philHealth; 
    } 

    public byte[] getPicture() { 
     return picture; 
    } 

    public void setPicture(byte[] picture) { 
     this.picture = picture; 
    } 

    public String getPlaceofBirth() { 
     return placeofBirth; 
    } 

    public void setPlaceofBirth(String placeofBirth) { 
     this.placeofBirth = placeofBirth; 
    } 

    public String getResidentialAddress() { 
     return residentialAddress; 
    } 

    public void setResidentialAddress(String residentialAddress) { 
     this.residentialAddress = residentialAddress; 
    } 

    public String getSssNo() { 
     return sssNo; 
    } 

    public void setSssNo(String sssNo) { 
     this.sssNo = sssNo; 
    } 

    public String getTelephone1() { 
     return telephone1; 
    } 

    public void setTelephone1(String telephone1) { 
     this.telephone1 = telephone1; 
    } 

    public String getTelephone2() { 
     return telephone2; 
    } 

    public void setTelephone2(String telephone2) { 
     this.telephone2 = telephone2; 
    } 

    public String getTin() { 
     return tin; 
    } 

    public void setTin(String tin) { 
     this.tin = tin; 
    } 

    public String getWeight() { 
     return weight; 
    } 

    public void setWeight(String weight) { 
     this.weight = weight; 
    } 

    public String getZipcode1() { 
     return zipcode1; 
    } 

    public void setZipcode1(String zipcode1) { 
     this.zipcode1 = zipcode1; 
    } 

    public String getZipcode2() { 
     return zipcode2; 
    } 

    public void setZipcode2(String zipcode2) { 
     this.zipcode2 = zipcode2; 
    } 

    @MapsId 
    public Clients getClients() { 
     return clients; 
    } 

    @MapsId 
    public void setClients(Clients clients) { 
     this.clients = clients; 
    } 

    public String getIdClient() { 
     return idClients; 
    } 

    public void setIdClients(String clients) { 
     this.idClients = clients; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (idPersonalInfo != null ? idPersonalInfo.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Personalinfo)) { 
      return false; 
     } 
     Personalinfo other = (Personalinfo) object; 
     if ((this.idPersonalInfo == null && other.idPersonalInfo != null) || (this.idPersonalInfo != null && !this.idPersonalInfo.equals(other.idPersonalInfo))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "com.doscst.profiling.database.Personalinfo[ idPersonalInfo=" + idPersonalInfo + " ]"; 
    } 

} 
+1

問題は挿入にありますが、挿入するコードやSQL自体を表示する必要があります。 –

+1

挿入しようとしている行は、おそらく外部キー制約に違反しています。その親は存在するのだろうか? –

+1

personalinfoとクライアントクラスを投稿してください。マッピングに問題があります。 –

答えて

0

PersonalInfoクラスでこれを試してみてください。

@OneToOne 
@JoinColumn(name = "idClients") 
private Clients clients; 
+0

試してみましたが、このエラーが発生します。書き込み可能と定義されているものは1つのみ、その他はすべて読み取り専用に指定する必要があります。 –

+0

あなたは例外またはスタックトレースを共有できますか。 –

+0

Exception [EclipseLink-48](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DescriptorException 例外説明:[personalinfo.IdClients]フィールドに複数の書き込み可能なマッピングが存在します。書き込み可能と定義できるのは1つだけです。他はすべて読み取り専用に指定する必要があります。 –

関連する問題