Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column';id';不能为空?_Java_Jpa_Mysql Insert Id - Fatal编程技术网

Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column';id';不能为空?

Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column';id';不能为空?,java,jpa,mysql-insert-id,Java,Jpa,Mysql Insert Id,我很难从java类向数据库表插入数据。我得到“内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'id'不能为null”。。。我做了一些挖掘,但没弄明白。让我发布我的代码。。。在此之前,我解释我想做什么。。。该项目包含Servlet类、JSP和java类。我使用JSP html表单获取数据并将其记录到数据库中。这里的代码 @Entity @Table(name="\"L

我很难从java类向数据库表插入数据。我得到“内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'id'不能为null”。。。我做了一些挖掘,但没弄明白。让我发布我的代码。。。在此之前,我解释我想做什么。。。该项目包含Servlet类、JSP和java类。我使用JSP html表单获取数据并将其记录到数据库中。这里的代码

    @Entity
    @Table(name="\"Leave\"")
    public class Leave implements Serializable{


/**
 * 
 */
private static final long serialVersionUID = 9088190714759727299L;

@Id
@Column(name="id",nullable=false,updatable=false)
private Long id;

//@OneToOne(mappedBy=)
@GeneratedValue(strategy = GenerationType.AUTO)
@JoinColumn(name = "person_id",referencedColumnName="id")
private Person person;


//@Enumerated(LeaveType)
@Column(name="leavetype")
private LeaveType leavetype;

@Temporal(TemporalType.DATE)
@Column(name="prepdate")
private Date prepdate;

@Temporal(TemporalType.DATE)
@Column(name="startdate")
private Date startdate;

@Temporal(TemporalType.DATE)
@Column(name="enddate")
private Date enddate;

@Temporal(TemporalType.DATE)
@Column(name="oldstartdate")
private Date oldStartdate;

@Temporal(TemporalType.DATE)
@Column(name="oldenddate")
private Date oldEnddate;

@Column(name="workday")
private String workday;

@Column(name="calendarday")
private String calendarday;

@Column(name="reason")
private String reason;

    getters setters....


还有错误

        [EL Info]: 2013-07-29 10:05:47.872--ServerSession(76232710)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2013-07-29 10:05:48.207--ServerSession(76232710)--file:/C:/Users/VAIO/Desktop/workspace - Kopya/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/EmployeeLeaveForm/WEB-INF/classes/_EmployeeLeaveForm login successful
[EL Warning]: 2013-07-29 10:05:48.292--UnitOfWork(1213364484)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
Error Code: 1048
Call: INSERT INTO `Leave` (id, calendarday, enddate, leavetype, oldenddate, oldstartdate, prepdate, reason, startdate, workday, person_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [11 parameters bound]
Query: InsertObjectQuery(com.eteration.leavesystem.model.Leave@4fff5f4f)
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
Error Code: 1048
Call: INSERT INTO `Leave` (id, calendarday, enddate, leavetype, oldenddate, oldstartdate, prepdate, reason, startdate, workday, person_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [11 parameters bound]
Query: InsertObjectQuery(com.eteration.leavesystem.model.Leave@4fff5f4f)
我还使用mysql并创建了一个数据库表

为什么我会遇到这些例外情况。。说真的,我挖了,但没什么用请帮我谢谢

编辑-


似乎您试图持久化您的实体,但其id仍然为空。这就是为什么会出现违反约束的错误

为了简单地解决此错误,我建议您尝试在id上使用自动递增选项创建数据库表。我想这应该可以解决您的问题。

这意味着为“id”列提供数据(来自某个源,并且类型与“id”的db字段类型相匹配),以便在调用时插入数据库。在代码中没有向查询提供和“id”数据的地方。

添加

@GeneratedValue(strategy = GenerationType.AUTO)
请假

@Id
@Column(name="id",nullable=false,updatable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

或者使用Long Id arg创建休假构造函数来设置您的Id。因此,您将无法创建Id设置为null的休假实例。

您没有在代码中设置Id,也没有告诉您的JPA提供商生成它

您可以使用
@GeneratedValue
注释通知您的JPA提供商生成您的id


这里有一个它是否在
id
字段中缺少
GeneratedValue
注释

如果使用
AUTO
生成,则id生成将取决于
持久性提供程序
。 您可能需要为主键设置自动增量

@Entity
@Table(name="Leave")
public class Leave implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id",nullable=false,updatable=false)
    private Long id;

    @OneToOne
    @GeneratedValue(strategy = GenerationType.AUTO) --> remove this code
    @JoinColumn(name = "person_id", referencedColumnName="id")
    private Person person;

}

我解决了我的问题谢谢你们的帮助。我用那种方式解决了我的问题。。。eclipselink需要我的数据库上的序列表,我创建了一个,然后它就可以工作了。。。再次感谢:)

看起来他甚至没有使用Hibernate;-)但是是的,Leave类中缺少GeneratedValue是的,u是正确的。我这样做了,我得到了这些错误:S,我不知道为什么它是序列。。我没有任何这样的表,也不想创建这样的表…@Pitch你不必使用
@GeneratedValue(strategy=GenerationType.SEQUENCE…
你可以简单地使用
@GeneratedValue(strategy=GenerationType.IDENTITY)
我遇到同样的错误”异常[EclipseLink-4002](Eclipse持久性服务-2.6.4.v20160829-44060b6):org.Eclipse.Persistence.exceptions.DatabaseException内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column'train_schedule_id'不能为空错误代码:1048调用:插入到train_站(id、日期、名称、序列、train_schedule__id)值(?,,,,,?,?)绑定=>[null,null,NY,null,null]查询:InsertObjectQuery(com.test.temp.core.data)。TrainstationData@12b0565e)你们能不能支持我把它弄明白……你们有序列表的DDL吗?
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
@Column(name="id",nullable=false,updatable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Entity
@Table(name="Leave")
public class Leave implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id",nullable=false,updatable=false)
    private Long id;

    @OneToOne
    @GeneratedValue(strategy = GenerationType.AUTO) --> remove this code
    @JoinColumn(name = "person_id", referencedColumnName="id")
    private Person person;

}