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

标签 java jpa entities

我的数据库中有两个表。表 1 包含 ID 和其他一些列。 Table2 是一个关系表,其中我将 table1 的 PK 作为 table2 中的 id,但我不使用 Table2 中的 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/

相关文章:

entities - 带老虎机的 RASA 故事

java - 在 Xerces 中从 DOM 获取 XML 实体替换文本

machine-learning - 如何使用 Google Dialogflow 从标题中提取屏幕尺寸

java - Gson 可以生成 XML 吗?

java - 如何在 Hibernate/JPA 中实现 "optional"关系?

jpa - 如何从JPA查询返回HashMap?

java - 多对多单向关系的 Spring Data JPA 规范

java - 如何在 Spring Security 中启用选择性 http 模式的内容安全策略

java - 正则表达式匹配除日期时间格式中包含的数字之外的所有数字

java - serversocket 上的最大套接字数