发生PersistenceException:org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新

时间:2013-11-20 06:48:07

标签: hibernate jdbc playframework playframework-1.x

我有一个Play Framework 1.2.7应用程序,有2个模型,课程和部门。 部门有很多课程,课程属于1个部门。当我尝试创建一个部门时,我收到此错误,PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

这是我的课程模型

package models;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

@Entity
public class Course extends Model {
public String CourseName;
public String CourseCode;

@Lob
public String CourseDescription;

@ManyToOne
public Department department;
public Course(String CourseName, String CourseCode, String CourseDescription, Department department){
    this.CourseName = CourseName;
    this.CourseCode = CourseCode;
    this.CourseDescription = CourseDescription;
    this.department = department;
}
}

这是我的部门模型

package models;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

@Entity
public class Department extends Model {
String DepartmentName;
String DepartmentCode;

@OneToMany
List<Course> courses;

public Department(String DepartmentName, String DepartmentCode){
    this.DepartmentName = DepartmentName;
    this.DepartmentCode = DepartmentCode;
}
public void addCourse(Course course){
    this.courses.add(course);
    this.save();
}
}

1 个答案:

答案 0 :(得分:0)

最近对您的课程所做的更新并未影响您的数据库。 您必须删除班级中的变量。代码是正确的,但在您的数据库中不必自动更新。这就是你有这个错误的原因。 作为解决方案:打开表格中提供的数据库课程将删除超出的列。

我已经在postgres中遇到了这个问题

PS:我的英语不太正确,因为我使用谷歌翻译。