【问题标题】:Insert row to table with the only identifier column with EclipseLink使用 EclipseLink 将行插入到具有唯一标识符列的表中
【发布时间】:2013-06-20 19:16:25
【问题描述】:

我有一个实体:

@Entity
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = Columns.ID)
    private Long id;

    // getter & setter
}

然后,我创建实体MyEntity my = new MyEntity(); 的实例,并希望使用DAO(使用JPA 的Spring Data)-dao.save(my); 保存它。我遇到了以下异常:

Caused by: Exception [EclipseLink-6023] 
(Eclipse Persistence Services - 2.4.0.v20120608-r11652): 
org.eclipse.persistence.exceptions.QueryException
Exception Description: The list of fields to insert into the table
[DatabaseTable(my_entity)] is empty.  
You must define at least one mapping for this table.
Query: InsertObjectQuery(null)

当我使用默认值向实体添加另一个无用的列时

private int nothing = 6;

异常消失。有什么解决办法吗?

This topic 对我没有帮助。我使用 EclipseLink + MySQL + Spring Data。

【问题讨论】:

    标签: java mysql jpa eclipselink spring-data-jpa


    【解决方案1】:

    这是因为您没有字段,并且使用的是生成的 id,因此没有可插入的内容。您要么需要添加另一个字段,要么需要使用 TABLE id 生成而不是 IDENTITY(我建议不要使用 IDENTITY,因为它不支持预分配)。

    【讨论】:

    • 有趣的是,在 Hibernate 中,OP 代码确实有效。我想知道 JPA 规范的哪一部分(如果有的话)完全禁止这样做。
    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 2013-06-04
    • 1970-01-01
    • 2021-03-29
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多