您能否看一下我的代码,当我尝试将记录插入数据库时​​,我不断收到以下异常:

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创建。
最后,我无法插入简单的记录...

谢谢

最佳答案

org.apache.openjpa.enhance.ReflectingPersistenceCapable


看来您正在使用openjpa.RuntimeUnenhancedClasses,并且该功能存在许多已知问题。如果其他人遇到此问题,请参阅OpenJPA Entity enhancement页以了解如何正确增强。

关于mysql - OpenJPA和MySQL auto_increment,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22821417/

10-11 12:18