エンティティをシームレスにローカライズする方法 いくつかのローカライズされたフィールドを持つオブジェクト (Entity) があり、これらのローカライズされたフィールドに次の方法でアクセスしたいと考えています: entity.getField()。
ローカリゼーションは、クライアントのコンテキストに基づいています。
これを実現するために、ローカライズされたフィールドに一時フィールドを使用し、onLoad メソッド内の Hibernate EmptyInterceptor の独自の実装を使用して、このフィールドにローカライズされた値を設定しています。ローカライズされた値は別のテーブルに保持されます。それは機能していますが、より良い解決策があるはずです。
@Entity
public class Card {
private Long id;
@Transient
private String name;
}
@Entity
public class LocalizedCard{
@Id
private Long id;
@ManyToOne
private Card card;
private Language language;
private String localisedName;
}
and the interceptor
public class MyInterceptor extends EmptyInterceptor {
@Override
public boolean onLoad(
Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
if (entity instanceof Card) {
Card c = (Card) entity;
c.setName(service.getLocalisedCardName());
return true;
}
return false;
}
}