JPA - Hibernate:在另一列上映射多个表

时间:2013-03-21 14:39:33

标签: java jpa hibernate-mapping

表格结构下的Conserider

country
=======
id
code
name_id

label
======
id
code
label_value_id

translations
=============
id
ref_id
language
value

现在我需要找到一个JPA映射来将country:name_idlabel:label_value_id映射到翻译ref_id。我一直在谷歌搜索正确的英语术语来解释这种情况,但我的空位正好点击。因此数据库可以保存记录,如本例所示

country
id: 1, code: BE, name_id: 30

label
id: 1, code: LABELA, label_value_id: 31

translations
id: 1, ref_id: 30, language: EN, value: BELGIUM
id: 2, ref_id: 30, language: NL, value: BELGIE
id: 3, ref_id: 31, language: EN, value: ALPHA_A
id: 4, ref_id: 31, language: NL, value: ALFA_A

在Java中我有3个类

国家/地区,标签和翻译,我将在国家/地区标签和翻译标签上建立@OneToMany关系,两者都应映射到ref_id,但我不知道如何将@OneToMany代码写入实现这一点。任何正确方向的提示都将非常受欢迎,一个解决方案或手册

=====

更新2013-03-23

如Joop所述,使用鉴别器是溶剂,但它不能开箱即用。我被迫使用hibernate注释@DiscriminatorOptions(force = true)。如果你不添加它,在获取所需的集合时,hibernate完全忽略了它的SQL查询中的Discriminator。

@Entity
@Table(name = "testtranslations")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorOptions(force=true)
public class TestTranslation extends DomainObject {

    /**
     * 
     */
    private static final long serialVersionUID = -6211853644196769521L;

    private long id;
    private String language;
    private String value;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="testtranslations_seq_gen") 
    @SequenceGenerator(name="testtranslations_seq_gen", sequenceName="TESTTRANSLATIONS_SEQ") 
    @Column(name="testtranslation_id")
    public long getId() {
        return id;
    }

    @Column(name="language", length=3, nullable=false)
    public String getLanguage() {
        return language;
    }

    @Column(name="value")
    public String getValue() {
        return value;
    }

    @Override
    public void setId(long id) {
        this.id = id;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public void setValue(String value) {
        this.value = value;
    }
}


@Entity
@Table(name = "testcountries")
public class TestCountry extends DomainObject {

    /**
     * 
     */
    private static final long serialVersionUID = -9207081478447113501L;

    private long id;
    private String code;
    private List<NameTranslation> name;
    private List<DescriptionTranslation> description;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="testcountries_seq_gen") 
    @SequenceGenerator(name="testcountries_seq_gen", sequenceName="TESTCOUNTRIES_SEQ") 
    @Column(name="country_id")
    public long getId() {
        return id;
    }

    @Column(name="iso_code", length=3, nullable=false)
    public String getCode() {
        return code;
    }

    @OneToMany(mappedBy="refId")
    public List<NameTranslation> getName() {
        return name;
    }

    @OneToMany(mappedBy="refId")
    public List<DescriptionTranslation> getDescription() {
        return description;
    }

    @Override
    public void setId(long id) {
        this.id = id;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setName(List<NameTranslation> name) {
        this.name = name;
    }

    public void setDescription(List<DescriptionTranslation> description) {
        this.description = description;
    }
}

@Entity
@DiscriminatorValue("NAMETRANSLATION")
public class NameTranslation extends TestTranslation {

    /**
     * 
     */
    private static final long serialVersionUID = 7197732491071768673L;

    private TestCountry refId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "refId", nullable=false)
    public TestCountry getRefId() {
        return refId;
    }

    public void setRefId(TestCountry refId) {
        this.refId = refId;
    }   
}

@Entity
@DiscriminatorValue("DESCTRANSLATION")
public class DescriptionTranslation extends TestTranslation {

    /**
     * 
     */
    private static final long serialVersionUID = -4128287237786410515L;

    private TestCountry refId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "refId", nullable=false)
    public TestCountry getRefId() {
        return refId;
    }

    public void setRefId(TestCountry refId) {
        this.refId = refId;
    }   
}

我做了所需的hibernate映射并编写了一个DbUnit测试来加载一个带有以下数据的id的TestCountry

<TESTCOUNTRIES COUNTRY_ID="1" VERSION="0" ISO_CODE="BE" />  
<TESTTRANSLATIONS TESTTRANSLATION_ID="1" VERSION="0" LANGUAGE="EN" VALUE="Belgium" REFID="1" DTYPE="NAMETRANSLATION" />
<TESTTRANSLATIONS TESTTRANSLATION_ID="2" VERSION="0" LANGUAGE="NL" VALUE="Belgie" REFID="1" DTYPE="NAMETRANSLATION" />
<TESTTRANSLATIONS TESTTRANSLATION_ID="3" VERSION="0" LANGUAGE="EN" VALUE="BelgiumDesc" REFID="1" DTYPE="DESCTRANSLATION" />
<TESTTRANSLATIONS TESTTRANSLATION_ID="4" VERSION="0" LANGUAGE="NL" VALUE="BelgieDesc" REFID="1" DTYPE="DESCTRANSLATION" />

我希望将来会帮助其他人,我很遗憾没有JPA sollution,我不得不被迫使用hibernate注释。

1 个答案:

答案 0 :(得分:1)

因为你有一个表1或表2的外键有点困难。

在JPA中确实存在 discriminator 概念,用于表继承。然后,从公共抽象表中导出两个表,并具有不同的鉴别器字段。一个example。虽然这有点不同。

P.S。使用搜索键discriminatorValue和discriminatorColumn寻找更好的例子。