OpenJPA和MySQL auto_increment

时间:2014-04-02 19:43:01

标签: mysql auto-increment openjpa

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

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

谢谢

2 个答案:

答案 0 :(得分:0)

这似乎是一个OpenJPA错误。经过几个小时的挫折之后,我使用EclipseLink测试了相同的代码,它运行得很完美。

我还了解到EclipseLink是JPA的参考实现。我会在这个项目上坚持下去。

答案 1 :(得分:0)

  

org.apache.openjpa.enhance.ReflectingPersistenceCapable

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