简体   繁体   English

休眠两个表和一个对象

[英]Hibernate two tables and one object

I have this situtation: 我有这样的说法:

Table1: 
tab_id
field11
field12


Table2
id
tab_id
field21
field22

I have to create one object on this two tables for example: 例如,我必须在这两个表上创建一个对象:

object: 

@Id
tabId

@Colummn(name="field11")
field11

@Colummn(name="field12")
field12

@Colummn(name="field21")
field21

When i update field21 table2 should update this field, but table1 doesn't have any information about table2, only table2 hat foreign key to table1 当我更新field21时table2应该更新此字段,但是table1没有有关table2的任何信息,只有table2会给table1带来外键

Any idea how i should this make ? 知道我应该怎么做吗?

I cannot change table structure, i can only create new class in java. 我无法更改表结构,只能在Java中创建新类。

The id column in Table2 (I guess it's the PK) is annoying. Table2id列(我想是PK)很烦人。 But if you can get it generated upon insert, mapping both tables using a @SecondaryTable should work: 但是,如果可以在插入时生成它,则可以使用@SecondaryTable映射两个表:

@Entity
@Table(name="TABLE1")
@SecondaryTable(name="TABLE2", pkJoinColumns = 
    @PrimaryKeyJoinColumn(name="TAB_ID", referencedColumnName="TAB_ID")
)
public class MyEntity {
    ...
    @Id @Column(name="TAB_ID")
    private Long tabId;

    @Column(name="FIELD11")
    private int field11;

    @Column(name="FIELD12")
    private int field12;

    @Column(name="FIELD21", table="TABLE2")
    private int field21;
    ...
}

If you can't, I'm afraid you'll have to map 2 classes (with a OneToOne relation). 如果不能,恐怕您将不得不映射2个类(具有OneToOne关系)。

References 参考文献

  • JPA Wikibook JPA Wikibook
  • JPA 1.0 specification JPA 1.0规范
    • Section 9.1.2 "SecondaryTable Annotation" 第9.1.2节“辅助表注释”
    • Section 9.1.32 "PrimaryKeyJoinColumn Annotation" 第9.1.32节“ PrimaryKeyJoinColumn注释”

您可以使用@SecondaryTable @SecondaryTable注释问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM