使用 JPA 持久保存 1 列表

发布于 2024-11-24 07:31:55 字数 1777 浏览 0 评论 0 原文

我有一张只有一列的表格。这是一个 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]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-12-01 07:31:55

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?

云淡风轻 2024-12-01 07:31:55

@GenerateValue(strategy=GenerationType.IDENTITY) 指示使用依赖于 IDENTITY 列的数据库支持

IDENTITY 列的概念并不存在于所有数据库中。例如,Apache Derby 和 Oracle 不支持这一点,而 MySQL、MSSQL(通常是 Sybase)则支持 IDENTITY 排序策略。您应该使用数据库支持的排序策略。为了可移植性,请选择 AUTOTABLE 排序策略。在大多数 JPA 提供程序中,AUTO 策略被实现为 TABLE 排序策略,因为所有数据库都支持创建用于维护序列值的表。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文