mysql - OpenJPA 和 MySQL 自动增量

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

谢谢

最佳答案

org.apache.openjpa.enhance.ReflectingPersistenceCapable

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

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

相关文章:

mysql ID自动递增不是从0开始

python - 如何创建不自增的主键?

mysql - 修改 AUTO_INCREMENT PRIMARY KEY 进行分区

java - 如何更改 JPA 生成的列名的默认格式

java - 如果应用程序 srv 隔离级别设置为 READ COMMITTED,是否会发生 OptimisticLockException?

MySQL 选择所有 DATETIME 字段不为 NULL 的地方

mysql - 有条件地删除光滑的表

sql - MySQL查询相关问题

dictionary - JPA 查询可以以 Java 映射的形式返回结果吗?

php - 如何使用具有外键的 Hydrator 并在 ZF2 中的 View 上显示两个实体属性