[英]shows error HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException:
I have problem with Spring MVC project.我对 Spring MVC 项目有疑问。 I want to insert in to values to database but when I click the submit button shows error HTTP Status 500 - Request processing failed;
我想将值插入到数据库中,但是当我单击提交按钮时显示错误 HTTP 状态 500 - 请求处理失败; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [sample.entity.Details]
嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法插入:[sample.entity.Details]
I don't know what's the problem.我不知道有什么问题。 please help me?
请帮我? the program given below
下面给出的程序
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Student Event Co-Ordinators/Organizer Details</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
<style>
body{
display: flex;
}
#submit
{
background-color: orange;
color:white;
height: 30px;
width:200px;
border: none;
}
input[type=text] input[type=email] input[type=number]{
transition: width 0.4s ease-in-out;
}
table {
border-collapse: collapse;
border: 1px solid black;
table-layout: auto;
width: 100%;
}
th,td {
border: 1px solid black;
width:16%
}
.btn {
border: none;
background-color: inherit;
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
display: inline-block;
}
.btn-add
{
background-color: orange;
color:white;
height: 40px;
width:200px;
border: none;
font-size: 12px;
}
/* On mouse-over */
.btn:hover {background: #eee;}
.btn-warning {color: orange;}
.btn-danger {color: red;}
.btn-info{
color:blue;
}
</style>
</head>
<body>
<div class="container">
<h3>Student Event Co-Ordinators/Organizer Details</h3>
<form id="studentevent" name="studentevent" action="/index.htm" method="post">
<input type="hidden" id="id" name="id" >
<div>
<div>
<input type="text" placeholder="Event name" name="name" required="" id="name">
<input type="email" placeholder="Event email id" name="email" required="" id="email">
<input type="number" placeholder="Registration fee" name="fee" required="" id="fee">
<input type="text" placeholder="Student name" name="sname" required="" id="sname">
<input type="number" placeholder="Student contact no" name="contactno" required="" id="contactno">
</div>
<br>
<div>
<input type="button" id="submit" value="submit" onclick="return addstudentevent();">
</form>
<br>
<br>
<table class="data-table" id="data-table">
<thead>
<th>Event Name</th>
<th>Email Id</th>
<th>Registration Fee</th>
<th>Student Name</th>
<th>Contact No</th>
<th> </th>
</thead>
<tbody id="displayBody">
</tbody>
</table>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script>
getall();
var isNew = true;
var studentid= null;
function addstudentevent()
{
var addId = $("#id").val();
var url = "";
var data = "";
var method;
if (isNew===true)
{
url = 'insert.htm';
data = $("#studentevent").serialize()+ "&Id=" + addId;
method = 'POST';
}
else
{
url = 'update.htm';
data = $("#studentevent").serialize() + "&Id=" + addId;
method = 'POST';
}
$.ajax({
type: method,
url : url,
dataType: 'JSON',
data : data,
success:function(data)
{
getall();
// $('id').val("");
$('#name').val("");
$('#email').val("");
$('#fee').val("");
$('#sname').val("");
$('#contactno').val("");
if(isNew === true)
{
alert("Data Inserted");
}
else
{
alert("Data Updated");
}
}
});
}
function getall()
{
$('#data-table').dataTable().fnDestroy();
$.ajax ({
url: "fetch.htm",
type: "GET",
dataType:"JSON",
success: function(data)
{
$("#data-table").dataTable({
"aaData":data,
"scrollX": true,
"aoColumns":
[
{"mData": "name"},
{"mData": "email"},
{"mData": "fee"},
{"mData": "sname"},
{"mData": "contactno"} ,
{ // " mData": "id",
mData:"id",
"render" : function(mData,type,row,meta)
{
return '<button class="btn btn-warning" onclick="get_details('+ mData +')">Edit</button><button class="btn btn-danger" onclick="get_delete('+ mData +')">Delete</button>';
}}
]
}); }
});
}
function get_details(id)
{
$.ajax({
type: "POST",
url: "edit.htm",
data: {"id": id},
success: function(data)
{
isNew = false;
var obj = JSON.parse(data);
studentid = obj[0].id;
$('#name').val(obj[0].name);
$('#email').val(obj[0].email);
$('#fee').val(obj[0].fee);
$('#sname').val(obj[0].sname);
$('#contactno').val(obj[0].contactno);
}
});
}
function get_delete(id)
{
$.ajax({
type:'POST',
url: 'delete.htm',
dataType: 'JSON',
data:{'id':id},
success:function (data)
{
alert("Data deleted");
getall();
}
});
}
</script>
</body>
</html>
studentcontroller.java学生控制器.java
package sample.controller;
import com.google.gson.Gson;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sample.dao.studentDao;
import sample.entity.Details;
/**
*
* @author admin
*/
@Controller
public class studentController {
@Autowired
studentDao stDao;
@RequestMapping(value ="index.htm", method = RequestMethod.GET)
public String index()
{
System.out.println("at index");
return "index";
}
@RequestMapping(value ="insert.htm", method = RequestMethod.POST)
public @ResponseBody String insert
(
@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("fee") String fee,
@RequestParam("sname") String sname,
@RequestParam("contactno") String contactno
)
{
Details ei= new Details();
ei.setName(name);
ei.setEmail(email);
ei.setFee(fee);
ei.setSname(sname);
ei.setContactno(contactno);
stDao.save(ei);
Gson ob = new Gson();
return "success";
}
@RequestMapping(value="fetch.htm", method = RequestMethod.GET)
public @ResponseBody String display()
{
List<Details> listdetails = (List<Details>) stDao.display();
return new Gson().toJson(listdetails);
}
@RequestMapping(value = "delete.htm")
public @ResponseBody String delete(@RequestParam("id") Integer id)
{
stDao.delete(id);
return "successful";
}
@RequestMapping(value = "edit.htm")
public @ResponseBody String edit(@RequestParam("id") Integer id)
{
List editlist;
editlist = stDao.getDetailsForEdit(id);
return new Gson().toJson(editlist);
}
@RequestMapping(value = "update.htm", method = RequestMethod.POST)
public @ResponseBody String update
(
@RequestParam(value="id",required=false) Integer id,
@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("fee") String fee,
@RequestParam("sname") String sname,
@RequestParam("contactno") String contactno
)
{
Details st = new Details();
st.setName(name);
st.setEmail(email);
st.setFee(fee);
st.setSname(sname);
st.setContactno(contactno);
System.out.println("st");
stDao.update(st);
Gson ob = new Gson();
return "success";
}
}
studentDao.java学生道.java
package sample.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import sample.entity.Details;
/**
*
* @author admin
*/
@Transactional
@Repository
public class studentDao {
@Autowired
private SessionFactory sessionFactory;
public void save(Details stdinfo) {
Session session = sessionFactory.openSession();
session.save(stdinfo);
session.close();
}
public List display() {
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Details.class);
criteria.setProjection(Projections.projectionList().add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
.add(Projections.property("email"), "email")
.add(Projections.property("fee"), "fee")
.add(Projections.property("sname"), "sname")
.add(Projections.property("contactno"), "contactno"));
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = criteria.list();
session.close();
return list;
}
public void delete(Integer id){
Session session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
Details stdtl = new Details(id);
session.delete(stdtl);
trx.commit();
session.close();
}
public List getDetailsForEdit(Integer id) {
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Details.class);
criteria.setProjection(Projections.projectionList()
.add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
.add(Projections.property("email"), "email")
.add(Projections.property("fee"), "fee")
.add(Projections.property("sname"), "sname")
.add(Projections.property("contactno"), "contactno")
);
criteria.add(Restrictions.eq("id", id));
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = criteria.list();
session.close();
return list;
}
public void update(Details Stdinfo){
Session session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
session.update(Stdinfo);
trx.commit();
session.close();
}
}
Details.java详情.java
package sample.entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
/**
*
* @author admin
*/
@Entity
@Table(name = "studentevent")
@NamedQueries({
@NamedQuery(name = "studentevent.findAll", query = "SELECT d FROM studentevent d")})
public class Details implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String[] name;
@Column(name = "email")
private String mail;
@Column(name = "fee")
private String fee;
@Column(name = "sname")
private String sname;
@Column(name = "contactno")
private String contactno;
public Details() {
}
public Details(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String[] getName() {
return name;
}
public void setName(String stdname) {
this.name = name;
}
public String getEmail() {
String email = null;
return email;
}
public void setEmail(String email) {
this.mail = email;
}
public String getFee() {
String fee = null;
return fee;
}
public void setFee(String fee) {
this.fee= fee;
}
public String getSname() {
String sname = null;
return sname;
}
public void setSname(String sname) {
this.sname= sname;
}
public String getContactno() {
return contactno;
}
public void setContactno(String contactno) {
this.contactno = contactno;
}
}
As @Lalit Mehra mentioned in the comments, HTML is sending a POST request and the controller is expecting a GET:正如@Lalit Mehra 在评论中提到的那样,HTML 正在发送 POST 请求,而 controller 正在等待 GET:
<form id="studentevent" name="studentevent" action="/index.htm" method="post">
@RequestMapping(value ="index.htm", method = RequestMethod.GET)
Make them match with the same request method and it will work.使它们与相同的请求方法匹配,它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.