gpt4 book ai didi

hibernate - Hibernate映射提供多语言支持

转载 作者:行者123 更新时间:2023-12-03 07:57:14 25 4
gpt4 key购买 nike

我有一个具有ID,名称,描述属性的Category类。

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

类和休眠映射的结构应该是什么?

这是我的带有注释的代码。但是我无法向数据库中插入任何内容:

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

}


我有两个表:
translation_string:tid,文本,langCode
类别:id,name_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=?


由于我的translation_string表没有Category_id或name_KEY字段,因此我认为映射存在问题。

我哪里错了?

最佳答案

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", 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());
}

关于hibernate - Hibernate映射提供多语言支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8863497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com