gpt4 book ai didi

java - Id 列来自其他表 JPA 实体

转载 作者:行者123 更新时间:2023-12-04 06:10:39 25 4
gpt4 key购买 nike

我的数据库中有两个表。带有 ID 和其他一些列的表 1。表 2 是一个关系表,其中我将表 1 中的 PK 作为表 2 中的 id 但我不使用表 2 中的 ID 并且它包含一些其他随机数据的值。

在 JPA 中,您必须使用 @Id 注释。我的问题是我想使用 table1 的实体作为 table2 实体的@Id。这是可能的,如果是,那么如何?

到目前为止的代码:
表格1

@Entity
@Table(name="Log", schema="logs")
@PersistenceUnit(name="domas")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="type", discriminatorType = DiscriminatorType.STRING)
public abstract class Log implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name="id", nullable=false)
public String id;

@Column(name="type", nullable=false)
@Enumerated(EnumType.STRING)
public LOG_TYPE type;
// and alot more not related code

表 2
@Entity
@Table(name="Log_Entity", schema="relations")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "relationType", discriminatorType = DiscriminatorType.STRING)
public abstract class LogRelation implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@OneToOne(mappedBy="id")
public Log log;

// and alot more not related code

最佳答案

如果您没有带有属性名称 id 和类型 LogRelation 的 @OneToOne 反面,则此mappedBy 没有意义:

@OneToOne(mappedBy="id")

遵循以下它将起作用:
@Id
@JoinColumn(name="lr_id")//or whatever column name you prefer
@OneToOne
Log log;

关于java - Id 列来自其他表 JPA 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822147/

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