我有一张只有一列的表格。这是一个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]
答案 0 :(得分:1)
DataNucleus可以保持该类正常。对于某些RDBMS而言,当没有指定列时,INSERT语句必须是特定类型(因为您的唯一列是在数据存储区中生成的),这可能是它无法做到的。在SQLServer的情况下,任何体面的JPA实现应该生成的语句是“INSERT INTO {tbl} DEFAULT VALUES”。也许得到一个实现这样做?
答案 1 :(得分:0)
@GeneratedValue(strategy=GenerationType.IDENTITY)
表示使用依赖于database support for IDENTITY
columns的排序策略。
IDENTITY
列的概念并不存在于所有数据库中。例如,Apache Derby和Oracle不支持这一点,而MySQL,MSSQL(通常是Sybase)支持IDENTITY排序策略。您应该使用数据库支持的排序策略。为了便于携带,请选择AUTO
或TABLE
排序策略。在大多数JPA提供程序中,AUTO
策略是作为TABLE
排序策略实现的,因为所有数据库都支持创建用于维护序列值的表。