用于多语言支持的 Hibernate 映射

发布于 2024-12-27 06:31:35 字数 2074 浏览 0 评论 0原文

我有一个 Category 类,其中包含 id、name、description 属性。

我希望用户可以输入多语言名称和描述并保存它们。

类的结构和 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;
}

}

我有两个表: 翻译后的字符串:tid、文本、语言代码 类别:id、name_id、description_id

我收到此信息消息: Hibernate:插入类别值 ( ) 当我想通过 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字段,我认为我的映射有问题。

我哪里错了?

I have a Category class which has id, name, description attributes.

I want that user can enter multilingual name and description and save them.

What should be the structure of the class and the hibernate mapping?

This is my code with annotations. But I couldn't insert anything to database:

@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;
}

}

I have two tables:
translated_string : tid, text, langCode
category: id, name_id, description_id

I am getting this info message:
Hibernate: insert into category values ( )
when I want to save the category by hibernateTemplate.

And when I want to find a category by id, it executes

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=?

Since my translated_string table doesn't have Category_id or name_KEY fields, i think I have a problem with the mapping.

Where am I wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2025-01-03 06:31:35
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);
    }
}

我的java的映射

<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>

有点生疏,请随意纠正语法

更新:简单字符串映射的注释,这应该足够了

@ElementCollection
@CollectionTable(name="TranslatedStrings", joinColumns=@JoinColumn(name="names_category_id"))
@Column(name="value")
public Map<String, String> getNames()

@ElementCollection
@CollectionTable(name="TranslatedStrings", joinColumns=@JoinColumn(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());
}
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);
    }
}

and the mapping

<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>

my java is a bit rusty, feel free to correct syntax

Update: annotations for simple string maps, which should suffice

@ElementCollection
@CollectionTable(name="TranslatedStrings", joinColumns=@JoinColumn(name="names_category_id"))
@Column(name="value")
public Map<String, String> getNames()

@ElementCollection
@CollectionTable(name="TranslatedStrings", joinColumns=@JoinColumn(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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文