我的数据库中有两个表。表 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/