3

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?

Burak
  • 5,706
  • 20
  • 70
  • 110
  • Good question! my guess is that you'll need to make one index for each language (unless you want to mix them), but no idea about the mapping, maybe using request_locale in the mapping file? – I.G. Pascual Jan 14 '12 at 16:27

1 Answers1

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

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());
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • I don't have 2 tables, I just have TranslatedStrings table. – Burak Jan 18 '12 at 18:54
  • http://stackoverflow.com/questions/6350415/internationalization-with-hibernate is the same topic. But I couldn't insert anyting. It is saying: Hibernate: insert into category values ( ) then gives error – Burak Jan 23 '12 at 21:16
  • Hibernate: insert into category values ( ) means there is no data to insert. And because my columns are NOT NULL, it gives an error. – Burak Jan 24 '12 at 08:57
  • looks like a different problem. does it insert when you comment out the maps? – Firo Jan 24 '12 at 10:22
  • I got this error when I wanted to insert without maps: Could not execute JDBC batch update; SQL [insert into translated_string (Category_id, description_KEY, langCode, text, tid) values (?, ?, ?, ?, ?)]; – Burak Jan 24 '12 at 10:41
  • how yould you possibly get an insert to translated strings when you save without maps??? – Firo Jan 24 '12 at 10:57
  • Sorry, I removed all maps. Just I am inserting emty category: Hibernate: insert into category values ( ) And I can see the row in the DB. – Burak Jan 24 '12 at 11:09