hi all,
is there a recommended way in Hibernate to store entites with multi-lingual values. My naive approach is to have an entity A that references an entity B (@OneToOne) containing the text and the languageId (see code example below). Then i could have a simple join from entity A to the entity B just specifying the desired language (my Repository implementation will add the language string from the current user session)
Code:
FROM Country c JOIN FETCH c.text t WHERE t.language='de'
But in order to have multiple text (B) entities for the same id i'd have to add another identifier to make it unique. Again, starting from a naive point of view, i created a composite key using the id and the language property. but if i do so Hibernate wont generate any ids for me because if you generate an unique id you don't need a composite key any more (and i do understand this :))
Is there any other way i could make this work? I thought of using a "pre-persist" listener to set the id manually but i still want Hibernate to generate an id (!) because Hibernate can do this in a DB independent way. Although i was able to create a Id generator myself i was not able to set it. (IdentifierGenerator always returns an id of type Serializable)
I appreciate any suggestions or hints in this direction.
Code:
@Entity
public class Country {
@Id @GeneratedValue
private Long id;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumns({
@JoinColumn(name="text_id",referencedColumnName="id"),
@JoinColumn(name="lang_id",referencedColumnName="language")})
private CountryText text;
@Column(name="text_id",updatable=false,insertable=false)
private Long textId; // need only textId to reload text
...
}
Code:
@Entity
public class CountryText {
private Long id;
private String language;
private String text;
}