【发布时间】:2014-02-07 20:18:21
【问题描述】:
我在 Spring/Hibernate 应用程序中设置 i18n 时遇到了困难。我现在尝试遵循这个食谱:http://www.theserverside.com/news/1377072/Internationalized-Data-in-Hibernate
只要我不使用二级缓存,它就可以工作。当我使用它时,第一个值被缓存并返回,即使我后来更改了语言。 (通过拆卸和组装方法)。我真的希望有人能帮我解决这个问题,因为我需要缓存 CarType 实体,而不是 userType 部分。 (I18nLocaleHolder 在内存映射上运行)
谢谢!
这里是实体类的一部分:
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Immutable
@Table(name = "CAR_TYPE")
public class CarType implements Serializable {
@Id
private Long id;
@Type(type = "LocalizedLabelUserType")
private String description;
...
这里是用户类型: 公共类 LocalizedLabelUserType 实现 UserType {
@Override
public int[] sqlTypes() {
return new int[] { Types.INTEGER };
}
@Override
public Class<String> returnedClass() {
return String.class;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return x == y;
}
@Override
public int hashCode(Object x) throws HibernateException {
return x == null ? 0 : x.hashCode();
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException,
SQLException {
Long labelId = (Long) LongType.INSTANCE.nullSafeGet(rs, names, session, owner);
return I18nLocaleHolder.getDescription(labelId, LocaleContextHolder.getLocale().getLanguage());
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException,
SQLException {
Long code = I18nLocaleHolder.getCode((String) value, LocaleContextHolder.getLocale().getLanguage());
LongType.INSTANCE.nullSafeSet(st, code, index, session);
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return value;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
@Override
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}
【问题讨论】:
标签: hibernate caching localization internationalization usertype