使用 Hibernate 进行国际化 [英] Internationalization with Hibernate

查看:20
本文介绍了使用 Hibernate 进行国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 hbm2ddl 生成类似的东西:

I would like to have something like this be generated from hbm2ddl:

______________    ______________       _______________
|Language    |    |I18N        |       |Test         |
--------------    --------------       ---------------
|iso3_code:PK|----|iso3_code:PK|       |test_id:PK   |
--------------    |i18n_id:PK  |-------|desc_i18n_id |
                  |i18n_text   |     |-|labl_i18n_id |
                  --------------       ---------------

这或多或少意味着,有一种表格语言,它保存了 iso 代码,也许还有其他一些信息.i18n 表在语言表上有一个外键 iso3_code,它也是一个主键.PK 的另一部分是 i18n_id.然后测试表在表 i18n 的字段 i18n_id 上有两个外键.

This means more or less that, there is a table language, which holds the iso code and maybe some other info. The i18n table has a foreign key iso3_code on the language table which is also a primary key. The other part of the PK is the i18n_id. The test table then has two foreign keys on the table i18n on the field i18n_id.

解析出来的hbm2ddl的输出应该是这样的:

The output of the parsed hbm2ddl should be like this:

public class Test  implements java.io.Serializable {
    private Integer testId;
    private Map<String,String> label = new HashMap<String,String>(0);
    private Map<String,String> description = new HashMap<String,String>(0);

    public Test() {
    }

    public Integer getTestId() {
        return this.testId;
    }

    public void setTestId(Integer testId) {
        this.testId = testId;
    }

    public Map<String, String> getLabel() {
        return label;
    }

    public void setLabel(Map<String,String> label) {
        this.label = label;
    }

    public Map<String, String> getDescription () {
        return description ;
    }

    public void setDescription (Map<String,String> description ) {
        this.description = description ;
    }

}

所以现在的问题是,我的 hbm.xml 文件如何生成这个表结构和这个类.即使我不能完全自动创建所有资源,我也很想知道应该如何声明它.我已经让它适用于选择,但不适用于插入或更新.

So now the question is, how has my hbm.xml file to look like to generate this table structure and this class. Even if i can not create all resources fully automatically, I would really like to know how this should be declared. I already got it to work for selects, but not for inserts or updates.

<class name="test.Test" table="test" catalog="testdb">
    <id name="testId" type="java.lang.Integer">
        <column name="test_id" />
        <generator class="native" />
    </id>
    <map name="label" table="i18n" fetch="join" cascade="all">
        <key column="i18n_id" not-null="true" foreign-key="label_id"/>
        <map-key column="iso3_code" type="string"/>
        <element column="i18n_text" type="string"/>
    </map>
</class>

<class name="test.Lang" table="lang" catalog="testdb">
    <id name="iso3Code" type="string">
        <column name="iso3_code" length="4" />
        <generator class="assigned" />
    </id>
</class>

<class name="test.I18n" table="i18n" catalog="testdb">
    <composite-id name="id" class="com.blazebit.test.I18nId">
        <key-property name="i18nId" type="int">
            <column name="i18n_id" />
        </key-property>
        <key-property name="iso3Code" type="string">
            <column name="iso3_code" length="4" />
        </key-property>
    </composite-id>
    <property name="i18nText" type="string">
        <column name="i18n_text" />
    </property>
</class>

我真的不知道为什么插入不起作用,但可能是因为无法生成应该识别文本的 I18nId 对象.在这种情况下,我也会接受这样的解决方案:映射 getLabel(){}

I do not really know why the insert does not work, but maybe it is because the I18nId object which should identify a text, can not be generated. In case of this, i would also accept a solution like this: Map getLabel(){}

但是使用这个解决方案会出现另一个问题,i18n_id 不能由 mysql 使用 auto_increment 设置.没有休眠也是可以的.

But with this solution another problem will arise, the i18n_id can not be set by mysql with auto_increment. It would be possible without hibernate.

请任何人帮助我或就如何实现这一点提供更好的实践!

Please anybody help me or give a better practice on how to implement this!

推荐答案

我知道我的问题很老了,但可能有人发现了这个并想知道该怎么做!

I know my question is very old, but probably someone finds this and wants to know how to do that!

我的最终解决方案是在地图中创建嵌入或复合元素.很简单,但你必须知道如何做到这一点.下面是一个如何使用注释的示例:

Well my final solution is creating embedded or composite elements within a map. Pretty easy, but you have to know how to do that. Here is an example on how to do that with annotations:

@Entity
@Table(name="A")
public class A implements Serializable{

    private Map<Locale, LocalizedA> localized = new HashMap<Locale, LocalizedA>();

    @ElementCollection
    @CollectionTable(name = "localized_a")
    @MapKeyJoinColumn(name = "field_name_for_locale")
    public Map<Locale, LocalizedA> getLocalized() {
        return this.localized;
    }

    public void setLocalized(Map<Locale, LocalizedA> localized) {
        this.localized = localized;
    }

}


@Embeddable
public class LocalizedA implements java.io.Serializable {

    @Column(name = "field_name_for_description")
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我希望这会对某人有所帮助,如果您想要 hbm.xml 文件的示例,只需发表评论,我会添加.

I hope this will help someone, if you want an example for hbm.xml files just comment and i will add that.

这篇关于使用 Hibernate 进行国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆