简体   繁体   中英

java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I am a beginner in spring mvc and was struck with a error but not able to resolve.can someone please help with the code.

JSP Page where we enter values for speaker details stored in database

   <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 CLASS

  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;
 }
  }

ERROR RESULT

java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails ( emsdataschema . ems_spkr_tbl , CONSTRAINT SPKR_EVNT_ID FOREIGN KEY ( SPKR_EVNT_ID ) REFERENCES ems_evnt_tbl_ai ( EVNT_ID ) ON DELETE CASCADE ON UPDATE CASCADE)

I entered refered values only but it is giving an exception :-( kindly check with the code and suggest the possible solution. Thanks in advance

Looks like eid is not specified or referenced non existing ID. Check it's not empty and contains ID which exists in the ems_evnt_tbl_ai table.

Could be even better to define Event entity and set proper relation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM