我希望从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_id上的表i18n上有两个外键。
解析后的hbm2ddl的输出应该是这样的:
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文件如何生成这个表结构和这个类。即使我不能完全自动创建所有资源,我真的想知道应该如何声明。我已经让它适用于选择,但不适用于插入或更新。
<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(){}
但是使用这个解决方案会出现另一个问题,mysql使用auto_increment无法设置i18n_id。没有休眠就可以。
请有人帮助我或更好地练习如何实现这一点!
答案 0 :(得分:8)
我知道我的问题已经很老了,但可能有人发现了这个并且想知道如何做到这一点!
我的最终解决方案是在地图中创建嵌入式或复合元素。很简单,但你必须知道如何做到这一点。以下是如何使用注释执行此操作的示例:
@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文件的例子只是评论,我会添加它。