2017-11-03 13 views
0

私は春のmvcの初心者ですが、エラーが発生しましたが解決できませんでした。java.sql.SQLIntegrityConstraintViolationException:子行を追加または更新できません:外部キー制約が失敗します

データベースに保存されている私たちは、スピーカーの詳細について値を入力JSPページ

<form class="form-horizontal bucket-form" id="myform" method="post" 
    action="spkr"> 
     <div class="control-label text-center"> 
      <p class="required"><em>required fields</em></p></div> 
       <div class="form-group">  
        <label class="col-sm-3 control-label required">First 
       Name</label> 
        <div class="col-sm-6"> 
         <input type="text" name="firstname" id="firstname" class="form-control"> 
        </div> 
       </div> 
       <div class="form-group">  
       <label class="col-sm-3 control-label required">Last Name</label> 
        <div class="col-sm-6"> 
         <input type="text" name="lastname" id="lastname" class="form-control"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label required">DOB</label> 
        <div class="col-sm-3"> 
         <input type="text" class="form-control" id="focusedInput" name="dob" type="text" value=""> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label required">Gender</label>      
         <span style="padding-left:15px"> 
          <label class="radio-inline"> 
           <input type="radio" name="gender" id="gender" > 
           Male 
          </label> 
          <label class="radio-inline"> 
           <input type="radio" name="gender" id="gender" > 
           Female 
          </label> 
         </span> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label required">Email id</label> 
        <div class="col-sm-6"> 
         <input type="email" class="form-control" placeholder="" name="mail" id="mail" > 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label required">Profession</label> 
        <div class="col-sm-6"> 
         <input type="text" class="form-control" placeholder="" name="prof" id="prof" > 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label ">Organisation</label> 
        <div class="col-sm-6"> 
         <input type="text" name="org" id="org" class="form-control" placeholder="eg:Tech Mahindra"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label ">Experience</label> 
        <div class="col-sm-6"> 
         <input type="number" class="form-control" min="0" name="exp" id="exp" placeholder=""> 
        </div> 
       </div> 

       <div class="form-group"> 
        <label class="col-sm-3 control-label required">Achievements</label> 
        <div class="col-sm-6"> 
         <textarea class="form-control" rows="5" name="ach" id="ach" placeholder=""></textarea> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-sm-3 control-label required">Event ID</label> 
        <div class="col-sm-3"> 
         <input type="number" class="form-control" name="eid" id="eid" > 
        </div> 
       </div> 

       <div class="form-group"> 
        <label class="col-sm-3 control-label">Upload image</label> 
        <div class="col-sm-6"> 
         <input type="file" class="form-control" id="forImage" accept="image/*"> 
        </div> 
       </div>     
       <div class="form-group"> 
        <div class="col-lg-offset-3 col-lg-6"> 
         <button class="btn btn-primary" id="speakerSubmit" type="submit">Save</button> 
         <button type="reset" class="btn btn" onClick="myFunction()">Reset</button> 
         <script type="text/javascript"> 
          function myFunction() { 
          document.getElementById("myform").reset(); 
          } 
         </script> 
        </div> 
       </div> 

      </form> 

POJOクラス

package com.ems.DO; 
    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.JoinColumn; 
    import javax.persistence.ManyToOne; 
    import javax.persistence.OneToOne; 
    import javax.persistence.Table; 
    @Entity 
    @Table(name="ems_spkr_tbl") 
    public class Speaker { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name="SPKR_ID",nullable=false) 
    private int speakerId; 
    @Column(name="FNAME") 
    private String firstname; 
    @Column(name="LNAME") 
    private String lastname; 
    @Column(name="GENDER") 
    private String gender; 
    /*private String DOB;*/ 
    @Column(name="SPKR_MAILID") 
    private String mail; 
    @Column(name="PROFESSION") 
    private String prof; 
    @Column(name="ORGN") 
    private String org; 
    @Column(name="EXP") 
    private String exp; 
    @Column(name="ACHIEVMNTS") 
    private String ach; 
    @Column(name="SPKR_EVNT_ID") 
    private int eid; 

    public Speaker(){ 

    } 

    public Speaker(int speakerId, String firstname, String lastname, String 
    gender, String mail, String prof,String org, String exp, String ach, 
    int eid) { 
     super(); 
     this.speakerId = speakerId; 
     this.firstname= firstname; 
     this.lastname = lastname; 
     this.gender = gender; 
     this.mail = mail; 
     this.prof = prof; 
     this.org = org; 
     this.exp = exp; 
     this.ach = ach; 
     this.eid = eid; 
    } 

    public int getSpeakerId() { 
     return speakerId; 
    } 

    public void setSpeakerId(int speakerId) { 
     this.speakerId = speakerId; 
    } 



    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 



    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 



    public String getGender() { 
     return gender; 
    } 

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



    public String getMail() { 
     return mail; 
    } 

    public void setMail(String mail) { 
     this.mail = mail; 
    } 



    public String getProf() { 
     return prof; 
    } 

    public void setProf(String prof) { 
     this.prof = prof; 
    } 



    public String getOrg() { 
     return org; 
    } 

    public void setOrg(String org) { 
     this.org = org; 
    } 



    public String getExp() { 
     return exp; 
    } 

    public void setExp(String exp) { 
     this.exp = exp; 
    } 



    public String getAch() { 
     return ach; 
    } 

    public void setAch(String ach) { 
     this.ach = ach; 
    } 




    public int getEid() { 
     return eid; 
    } 

    public void setEid(int eid) { 
     this.eid = eid; 
    } 
    } 

SpeakerService

package com.ems.service; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import com.ems.DO.Speaker; 
import com.ems.dao.SpeakerDao; 
@Repository 
public class SpeakerServiceImpl implements SpeakerService{ 

@Autowired 
SpeakerDao speakerDao; 

public SpeakerServiceImpl() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

public boolean add(Speaker s) { 
    boolean status=speakerDao.add(s); 
    return status; 

} 
} 

SpeakerDaoClass

package com.ems.dao; 
    import java.util.List; 
    import org.hibernate.Query; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Repository; 
    import org.hibernate.cfg.Configuration; 

    import com.ems.DO.Speaker; 

@Repository 
public class SpeakerDaoImpl implements SpeakerDao{ 

    @Autowired 
    private SessionFactory sessionFactory; 


public SpeakerDaoImpl(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 


public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
} 

public boolean add(Speaker s) { 

Session session = sessionFactory.getSessionFactory().openSession(); 
session.save(s); 
System.out.println("one record inserted"); 
return true; 
} 
    } 

エラー結果

java.sql.SQLIntegrityConstraintViolationException:子行を追加したり、 更新することはできません:外部キー制約は (emsdataschemaを失敗しました。 ems_spkr_tbl、CONSTRAINT SPKR_EVNT_ID FOREIGN KEY (SPKR_EVNT_ID)UPDATE CASCADE ON DELETE CASCADE ON ems_evnt_tbl_aiEVNT_ID)を参照)

Iのみ参照さの値を入力したが、それは :-(例外 を与えている親切コードを確認そして、。eidは、指定されたまたは非既存のIDで参照されていないよう

+0

ddlステートメントを取得してここに投稿してください。 例: - create create table <データベース名>。

; –

答えて

1

が見える事前に 感謝します。可能な解決策を提案し、それが空ではないとに存在するIDが含まれているチェック0テーブル。

Eventエンティティを定義して適切な関係を設定する方が良いでしょう。

関連する問題