使用 JPA 持久保存 1 列表
我有一张只有一列的表格。这是一个 Id
我怎样才能用 JPA 持久化它?
我尝试过 entityManager.persist(new OneColumnTable());
并且它抛出 PersistenceException
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-6023] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.QueryException
Exception Description: **The list of fields to insert into the table [DatabaseTable(OneColumnTable)] is empty. You must define at least one mapping for this table.**
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:747)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.flush(EntityManagerWrapper.java:418)
我该怎么办?
更新
@Entity
@Table(name = "OneColumnTable")
public class OneColumnTable implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "OneColumn")
private Integer oneColumn;
public OneColumnTable() {
}
public Integer getOneColumn() {
return oneColumn;
}
public void setOneColumn(Integer oneColumn) {
this.oneColumn= oneColumn;
}
}
表格
USE [myDB]
GO
/****** Object: Table [dbo].[OneColumnTable] Script Date: 07/15/2011 12:10:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[OneColumnTable](
[OneColumn] [bigint] IDENTITY(1,1) NOT NULL,
PRIMARY KEY CLUSTERED
(
[OneColumn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DataNucleus 可以很好地保留该类。问题是,对于某些 RDBMS,当未指定列时,INSERT 语句必须是特定类型(因为您唯一的列是在数据存储中生成的),而这可能是它无法做到的。对于 SQLServer,任何合适的 JPA 实现都应该生成的语句是“INSERT INTO {tbl} DEFAULT VALUES”。也许得到一个可以做到这一点的实现?
DataNucleus can persist that class fine. The thing is for some RDBMS the INSERT statement has to be of a particular type when no columns are being specified (since your only column is being generated in the datastore), and this is presumably what it can't do. In the case of SQLServer the statement that any decent JPA implementation ought to generate is "INSERT INTO {tbl} DEFAULT VALUES". Perhaps get an implementation that does that?
@GenerateValue(strategy=GenerationType.IDENTITY)
指示使用依赖于 对IDENTITY
列的数据库支持。IDENTITY
列的概念并不存在于所有数据库中。例如,Apache Derby 和 Oracle 不支持这一点,而 MySQL、MSSQL(通常是 Sybase)则支持 IDENTITY 排序策略。您应该使用数据库支持的排序策略。为了可移植性,请选择AUTO
或TABLE
排序策略。在大多数 JPA 提供程序中,AUTO
策略被实现为TABLE
排序策略,因为所有数据库都支持创建用于维护序列值的表。@GeneratedValue(strategy=GenerationType.IDENTITY)
indicates the use of a sequencing strategy that is dependent on the database support forIDENTITY
columns.The concept of
IDENTITY
columns is not present in all databases. For instance, Apache Derby and Oracle do not support this, while MySQL, MSSQL (and typically, Sybase), supports the IDENTITY sequencing strategy. You should be using a sequencing strategy that is supported by your database. For portability, choose theAUTO
orTABLE
sequencing strategies. In most JPA providers, theAUTO
strategy is implemented as theTABLE
sequencing strategy, as all databases support the creation of a table for maintaining the sequence values.