2011-10-19 65 views
2

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

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

到目前为止的代码: 表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 
+0

你想制作另一个表格的表格ID吗?或者你是否试图从一个表中选择一些字段,并尝试为另一个表形成__复合主键___?如果是,那么你应该检查注解'@ Embeddable'和'@ EmbeddedId'。 –

+0

我只是试图从Table1中获取PK到Table2中,因此我不必在table2实体上声明Id注释,因为我没有Table2中的真实ID列,只有Table1中的refId。但JPA迫使我使用Id注释,所以即时尝试解决它 – Marthin

回答

3

此的mappedBy是没有意义的,如果你没有@OneToOne属性名ID和类型LogRelation反侧:

@OneToOne(mappedBy="id") 

随着它将工作:

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

这个类[com.foo.bar]没有定义一个IdClass将被生成。 – user2083529

相关问题