我是春季mvc的初学者并且遇到了错误但无法解决。有人请帮忙解释一下代码。
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 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;
}
}
错误结果
java.sql.SQLIntegrityConstraintViolationException:无法添加或 更新子行:外键约束失败 (
emsdataschema
。ems_spkr_tbl
,CONSTRAINTSPKR_EVNT_ID
FOREIGN KEY (SPKR_EVNT_ID
)REFERENCESems_evnt_tbl_ai
(EVNT_ID
)ON DELETE CASCADE ON UPDATE CASCADE)
我只输入了引用的值,但它给出了一个例外 :-( 请仔细检查代码并提出可能的解决方案。 提前致谢
答案 0 :(得分:1)
看起来eid
未指定或引用非现有ID。检查它是否为空并且包含ems_evnt_tbl_ai
表中存在的ID。
可以更好地定义Event
实体并设置正确的关系。