persist a 1 column table using JPA
I've got a table with only one column. It's an Id
How can I persist it with JPA?
I've tried a entityManager.persist(new OneColumnTable());
and it throws a 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)
How can I do it?
UPDATE
@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;
}
}
TABLE
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]
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?
@GeneratedValue(strategy=GenerationType.IDENTITY)
indicates the use of a sequencing strategy that is dependent on the database support for IDENTITY
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 the AUTO
or TABLE
sequencing strategies. In most JPA providers, the AUTO
strategy is implemented as the TABLE
sequencing strategy, as all databases support the creation of a table for maintaining the sequence values.
精彩评论