【问题标题】:OpenJPA and MySQL auto_incrementOpenJPA 和 MySQL 自动增量
【发布时间】: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


【解决方案1】:

org.apache.openjpa.enhance.ReflectingPersistenceCapable

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

【讨论】:

  • 嗨@Rick,是的,我正在使用该属性,我的意思是,我是。 Netbeans 与 EclipseLink 实现有很好的集成,并且运行良好。谢谢你的链接,我一定会看看的。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多