Hibernate的国际化 [英] Internationalization with Hibernate
问题描述
______________ ______________ _______________
| Language | | I18N | |测试|
-------------- -------------- ---------------
| 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_id字段的表i18n上有两个外键。
解析后的hbm2ddl的输出应该是这样的:
public class Test实现java.io.Serializable {
private Integer testId;
私人地图< String,String> label = new HashMap< String,String>(0);
私人地图< String,String> description = new HashMap< String,String>(0);
$ b $ public Test(){
}
public Integer getTestId(){
return this.testId;
}
public void setTestId(Integer testId){
this.testId = testId;
}
public Map< String,String> getLabel(){
返回标签;
}
public void setLabel(Map< String,String> label){
this.label = label;
}
public Map< String,String> getDescription(){
返回描述;
public void setDescription(Map< String,String> description){
this.description = description;
}
}
所以现在的问题是,有我的hbm.xml文件看起来像生成这个表结构和这个类。即使我不能完全自动创建所有资源,我也很想知道应如何声明。
< class name =test.Test我已经将它用于选择,但不适用于插入或更新。 table =testcatalog =testdb>
< id name =testIdtype =java.lang.Integer>
< column name =test_id/>
< generator class =native/>
< / id>
< key column =i18n_idnot-null =trueforeign-key =label_id/>
< map-key column =iso3_codetype =string/>
< element column =i18n_texttype =string/>
< / map>
< / class>
< class name =test.Langtable =langcatalog =testdb>
< id name =iso3Codetype =string>
< column name =iso3_codelength =4/>
< generator class =assigned/>
< / id>
< / class>
< class name =test.I18ntable =i18ncatalog =testdb>
< composite-id name =idclass =com.blazebit.test.I18nId>
< key-property name =i18nIdtype =int>
< column name =i18n_id/>
< / key-property>
< key-property name =iso3Codetype =string>
< column name =iso3_codelength =4/>
< / key-property>
< / composite-id>
< property name =i18nTexttype =string>
< column name =i18n_text/>
< / property>
< / class>
我不知道为什么插入不起作用,但也许是因为I18nId对象应该识别一个文本,不能生成。在这种情况下,我也会接受这样的解决方案:
Map getLabel(){}
但是这个解决方案会出现另一个问题,i18n_id不能由具有auto_increment的mysql设置。这可能没有冬眠。
请任何人帮助我或给如何实现这个更好的做法!
我知道我的问题很古老,但可能有人发现这一点,并想知道如何做到这一点!
我的最终解决方案是在地图中创建嵌入或复合元素。很容易,但你必须知道如何做到这一点。这里有一个关于如何使用注释做到这一点的例子:
@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
公共类LocalizedA实现java.io.Serializable {
@Column(name =field_name_for_description)
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
$ / code $ / pre
我希望这会帮助别人,如果你想要例如hbm.xml文件只是评论,我会补充说。
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 |
-------------- ---------------
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.
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 ;
}
}
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>
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(){}
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;
}
}
I hope this will help someone, if you want an example for hbm.xml files just comment and i will add that.
这篇关于Hibernate的国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!