2012-01-14 69 views
3

我有一個具有id,name,description屬性的Category類。支持多語言的Hibernate映射

我想讓用戶可以輸入多種語言名稱和說明並保存它們。

應該是什麼類的結構和hibernate映射?

這是帶註釋的代碼。但我不能插入什麼數據庫:

@Entity 
@Table(name = "category") 
public class Category { 
@Id 
@GeneratedValue 
public Integer getId() { 
    return id; 
} 
public void setId(Integer id) { 
    this.id = id; 
} 
private Integer id; 
private Map<String, TranslatedString> name; 
private Map<String, TranslatedString> description; 



@ElementCollection 
@CollectionTable(name = "translated_string") 
@MapKeyJoinColumn(name = "langCode") 
public Map<String, TranslatedString> getName() { 
    return this.name; 
} 

public void setName(Map<String, TranslatedString> name) { 
    this.name = name; 
} 

@ElementCollection 
@CollectionTable(name = "translated_string") 
@MapKeyJoinColumn(name = "langCode") 
public Map<String, TranslatedString> getDescription() { 
    return this.description; 
} 

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



} 


@Embeddable 
public class TranslatedString { 

public Integer getTid() { 
    return tid; 
} 

public void setTid(Integer tid) { 
    this.tid = tid; 
} 

private Integer tid; 



private String langCode; 

@Column(name = "langCode") 
public String getLangCode() { 
    return langCode; 
} 

public void setLangCode(String langCode) { 
    this.langCode = langCode; 
} 

private String text; 

@Column(name = "text") 
public String getText() { 
    return text; 
} 

public void setText(String text) { 
    this.text = text; 
} 

} 

我有兩個表: translated_string:TID,文字的langCode 類:身份證,名_ID,description_id

我得到這個信息的消息: 休眠:當我想通過hibernateTemplate保存類別時,插入類別值() 。

當我想找到ID的類別,它執行

select 

name0_.Category_id as Category1_0_0_, 
name0_.langCode as langCode0_, 
name0_.text as text0_, 
name0_.tid as tid0_, 
name0_.name_KEY as name5_0_ 

from translated_string name0_ 

where name0_.Category_id=? 

由於我translated_string表沒有CATEGORY_ID或name_KEY領域,我想我已經用映射的一個問題。

我在哪裏錯了?

+0

好問題!我的猜測是你需要爲每種語言創建一個索引(除非你想混合它們),但不知道映射,可能在映射文件中使用request_locale? – 2012-01-14 16:27:58

+0

什麼是TransaltedString? – Firo 2012-01-18 11:09:08

回答

1
class Category { 
    public int Id; 
    public Map<Language, String> names; 
    public Map<Language, String> descriptions; 

    public String getCurrentName() 
    { 
     // get current Language from somewhere 
     return names.GetValue(currentLanguage); 
    } 
} 

和映射

<map name="names" table="names"> 
    <key column="categoryid" /> 
    <index column="language_id" /> 
    <element column="value"/> 
</map> 

<map name="descriptions" table="descriptions"> 
    <key column="categoryid" /> 
    <index column="language_id" /> 
    <element column="value" length="whatever"/> 
</map> 

我的Java是一個有點生疏,隨時糾正語法

更新:註解簡單的字符串的地圖,這應該足夠了

@ElementCollection 
@CollectionTable(name="TranslatedStrings", [email protected](name="names_category_id")) 
@Column(name="value") 
public Map<String, String> getNames() 

@ElementCollection 
@CollectionTable(name="TranslatedStrings", [email protected](name="descriptions_category_id")) 
@Column(name="value") 
public Map<String, String> getDescriptions() 


public String getCurrentName() 
{ 
    // get current Language from somewhere 
    return names.GetValue(currentLanguage.getCode()); 
} 
+0

我沒有2個表格,我只有TranslatedStrings表格。 – Burak 2012-01-18 18:54:30

+0

http://stackoverflow.com/questions/6350415/internationalization-with-hibernate是同一主題。但我無法插入任何。它說:休眠:插入類別值() 然後給出錯誤 – Burak 2012-01-23 21:16:45

+0

什麼錯誤?名稱,消息和/或堆棧跟蹤將有所幫助 – Firo 2012-01-24 08:40:45