当数据库表已填充JPA时如何正确设置@Id字段

发布于 2024-12-15 06:58:19 字数 243 浏览 5 评论 0原文

我的数据库中有一个表,并且我在该字段上添加了 @Id 属性。作为策略,我使用GenerationType.IDENTITY。当数据库表尚未由 SQL 脚本中的行填充时,此方法可以正常工作。

当表中已经有一些行时,如何设法让它工作?因为当我尝试从应用程序中预填充实体时它不起作用。

我使用 Derby 数据库来实现此目的,并使用 eclipselink 作为实现。

I am having a single table in my database and I have added the @Id attribute over the field. As strategy I use GenerationType.IDENTITY. This works fine WHEN the database table is not already populated by rows from a SQL script.

How can I manage to get it to work when the table already has some rows in it? Because it doesn't work when I am trying to insert entities from my application when it is pre-populated.

I am using Derby database for this and eclipselink as implementation.

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

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

发布评论

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

评论(2

梦途 2024-12-22 06:58:19

使用序列生成器从序列表中获取其 id。像这样的事情:

@Id
@GeneratedValue(generator = "yourTableIdGenerator")
@GenericGenerator(name = "yourTableIdGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
                    @Parameter(name = "sequence_name", value = "your_table_id_seq"),
                    @Parameter(name = "optimizer", value = "hilo"),
                    @Parameter(name = "initial_value", value = "1000"),
                    @Parameter(name = "increment_size", value = "10") }
                )
@Column(name = "your_table_id", length = 15)
public Long getId() {
    return id;
}

将initial_value设置为大于从脚本填充的行的最大id的值。

从 java 6 ee api:http://download.oracle。 com/javaee/6/api/javax/persistence/TableGenerator.html

@TableGenerator(
   name="empGen",
   table="ID_GEN",
   pkColumnName="GEN_KEY",
   valueColumnName="GEN_VALUE",
   pkColumnValue="EMP_ID",
   initialValue = 1000,
   allocationSize=1)
@Id
@GeneratedValue(strategy=TABLE, generator="empGen")
int id;

Use sequence generator that gets its ids from a sequence table. Something like this:

@Id
@GeneratedValue(generator = "yourTableIdGenerator")
@GenericGenerator(name = "yourTableIdGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
                    @Parameter(name = "sequence_name", value = "your_table_id_seq"),
                    @Parameter(name = "optimizer", value = "hilo"),
                    @Parameter(name = "initial_value", value = "1000"),
                    @Parameter(name = "increment_size", value = "10") }
                )
@Column(name = "your_table_id", length = 15)
public Long getId() {
    return id;
}

Set the initial_value to a value larger than the maximum id of the rows populated from the script.

From the java 6 ee api: http://download.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html

@TableGenerator(
   name="empGen",
   table="ID_GEN",
   pkColumnName="GEN_KEY",
   valueColumnName="GEN_VALUE",
   pkColumnValue="EMP_ID",
   initialValue = 1000,
   allocationSize=1)
@Id
@GeneratedValue(strategy=TABLE, generator="empGen")
int id;
弃爱 2024-12-22 06:58:19

我假设您的错误是因为您为表创建的 IDENTITY (例如)以 "1" 开头,并且您已经使用 id "1" 的行填充了表。所以肯定存在 PK 约束违规。如果是这种情况,我可以建议:

  1. 检查预填充数据的行数。以 3500 为例。
  2. 在更高的数字中创建标识列以避免违反 PK 约束,例如下表以 4000 开始标识:

这应该在 Derby DB 中工作

CREATE TABLE MAPS    (
    MAP_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 4000, INCREMENT BY 1),
    MAP_NAME VARCHAR(24) NOT NULL,
    REGION VARCHAR(26),
    AREA DECIMAL(8,4) NOT NULL,
    PHOTO_FORMAT VARCHAR(26) NOT NULL,
    PICTURE BLOB(102400),
    UNIQUE (MAP_ID, MAP_NAME)
)

I will assume that your error is because the IDENTITY that you has created for you table starts (by example) in "1" and you have already populated the table with a row with the id "1" . So sure exist a PK Constraint violation. If this is the scenario I can suggest:

  1. Check the number of rows for prepopulate the data. By example 3500.
  2. Create the identity column in a superior number for avoid PK constraint violation, by example the following table starts the identity in 4000:

This should work in Derby DB

CREATE TABLE MAPS    (
    MAP_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 4000, INCREMENT BY 1),
    MAP_NAME VARCHAR(24) NOT NULL,
    REGION VARCHAR(26),
    AREA DECIMAL(8,4) NOT NULL,
    PHOTO_FORMAT VARCHAR(26) NOT NULL,
    PICTURE BLOB(102400),
    UNIQUE (MAP_ID, MAP_NAME)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文