【发布时间】:2014-04-02 19:43:00
【问题描述】:
请您看一下我的代码,当我尝试将记录插入数据库时,我不断收到以下异常:
Apr 02, 2014 1:30:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [UserEditorServlet] in context with path [/My.Web] threw exception
<openjpa-2.2.2-r422266:1468616 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: error during validation of <unknown>
我的表是这样定义的:
create table User (
id bigint primary key not null auto_increment,
name varchar(200),
status varchar(30),
createdOn datetime,
modifiedOn datetime,
password varchar(60),
index(status)
) ENGINE=InnoDB;
我的身份字段是这样的:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
protected Long id = null;
事实证明,如果我为我的 id 分配一个值,我的异常会更改为:
Apr 02, 2014 1:37:18 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [UserEditorServlet] in context with path [/My.Web] threw exception
<openjpa-2.2.2-r422266:1468616 fatal user error> org.apache.openjpa.persistence.InvalidStateException: Primary key field net.jaimes.Persistent.id of org.apache.openjpa.enhance.ReflectingPersistenceCapable@194765c4 has non-default value. The instance life cycle is in PNewState state and hence an existing non-default value for the identity field is not permitted. You either need to remove the @GeneratedValue annotation or modify the code to remove the initializer processing.
在调试的时候,我只有一个空变量:Id,预计是MySQL创建的。 最后,我无法插入简单的记录...
谢谢
【问题讨论】:
-
请发布您的 persistence.xml 的内容。
标签: mysql auto-increment openjpa