Hi,
I have a problem with mapping to legacy database. The database contains a number of dictionary tables with localized data:
Code:
table group_types (
id bigint not null,
icon_id bigint not null, // some foreign key
en_US varchar(255), // english
de varchar(255), // german
es varchar(255), // spanish
/// etc
)
I also have a hierarchy of Dictionary classes:
Code:
@MappedSuperclass
public abstract class Dictionary {
private Long id;
private String name;
// getters and setters
}
@Entity
@Table(name="group_types")
public class GroupType extends Dictionary {
@ManyToOne
@JoinColumn(name="icon_id")
private Icon icon;
// more getters and setters
}
I'd like to map
group_types table to a class
GroupType and choose in runtime what locale column (
en_US,
en_UK,
de,
fr etc) to bind to
name field.
I cannot use native queries, because I need to get correct localized name everywhere (I need EAGER fetch of group type in group, for example).
I see the only option (except changing the DB schema which is not possible): to put somewhere in Hibernate a column picker object that will be called from a query generator and will look something like this:
Code:
class LocaleColumnPicker {
Map<String,String> columns = ...;
public String getColumnName(String field) {
if ("name".equals(field)) return LocaleProvider.getLocale().toString();
return columns.get(field);
}
}
I'm using Spring 2.5 + JPA (Annotations 3.3.0, EntityManager 3.3.1) + Hibernate 3.2.4 (Criteria API in some cases)
Has someone ideas, how I could do that?