gpt4 book ai didi

java - JPA - 创建临时主键

转载 作者:行者123 更新时间:2023-11-30 03:30:13 25 4
gpt4 key购买 nike

我有一个映射到现有表的实体类。该表没有主键。这是表格:

CREATE TABLE [dbo].[testtable](
[field1] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[field2] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[field3] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[field4] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[field5] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[field6] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)

我无法对此表进行任何更改,我正在尝试通过仅在内存中设置主键来找到使用 jpa 的方法。我理论上知道,如果我只想在内存中有一列,我可以使用 transient 注释。我想将 field1, field2, field3 设置为指向现有数据库字段的临时主键。像指针这样的东西,以便能够获取现有字段的值。这可能吗?

这是实体类:

@Entity
@Table(name = "testtable")
public class TestJPA implements Serializable
{
@Column(name = "field1")
private String field1;

@Column(name = "field2")
private String field2;

@Column(name = "field3")
private String field3;

@Column(name = "field4")
private String field4;

@Column(name = "field5")
private String field5;

@Column(name = "field6")
private String field6;
}

最佳答案

yes. field1 field2 and field3 can identify uniquely a record

在这种情况下,请考虑使用 @EmbeddedId进行复合 PK。

编辑:超链接已更新

根据 Oracle specification :

Each JPA entity must have a primary key that uniquely identifies it from other instances. The primary key (or the fields contained within a complex primary key) must be persistent fields.

关于java - JPA - 创建临时主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com