Id列来自其他表JPA实体

时间:2011-10-19 13:35:55

标签: java jpa entities

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

在JPA中,您必须使用@Id注释。我的问题是我想使用table1的实体作为tableI实体的@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

2 个答案:

答案 0 :(得分:3)

如果没有带属性名称id的@OneToOne反面并且键入LogRelation,则此mappedBy没有意义:

@OneToOne(mappedBy="id")

随着它将起作用:

@Id
@JoinColumn(name="lr_id")//or whatever column name you prefer
@OneToOne
Log log;

答案 1 :(得分:0)

你的意思是“复合身份”,其中一个对象的身份是另一个对象的身份(的一部分)。 http://www.datanucleus.org/products/accessplatform_3_0/jpa/orm/compound_identity.html#1_1_uni