Hi, I'm trying to achieve i18n with Hibernate UserTypes and Spring for DaoSupport (Thus my DAO's are Spring singleton beans).
Let me first explain how am I using usertypes to do i18n:
1. I have a product with a description that needs to be loaded with the right language according to the user locale :
Code:public class Product{ ... private String description; //Getter and setter ... }
2. I have a user type to let hibernate load the appropriate string "transparently" : (unrelevant methods are not shown )
LocaleUtil.currentLocale() just returns a java.util.Locale object.Code:public class LocalizedLabelUserType implements UserType { public int[] sqlTypes() { return new int[]{Types.BIGINT}; } public Class returnedClass() { return String.class; } public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { Long labelId = (Long) Hibernate.STRING.nullSafeGet(rs, names); return LocalizedLabelUtil.getText(labelId, LocaleUtil.currentLocale()); } public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { Long labelId = LocalizedLabelUtil.getId( (String) value, LocaleUtil.currentLocale()); Hibernate.LONG.nullSafeSet(st, labelId, index); } // unrelevant methods here ... }
I'll talk about LocalizedLabelUtil shortly.
3. Using EJB3 annotations I set the above usertype on my product description:
Code:@Type(type="LocalizedLabelUserType") private String description;
4. Additionnaly I have a LocalizedLabel pojo which is mapped to a LOCALIZEDLABELS table and a LocalizedLabelDao which is a spring singleton extending HibernateDaoSupport.
The LocalizedLabelUtil sould access the dao to retrieve the appropriate text or id from the LOCALIZEDLABELS table.
But this is where it is unclear to me, how could I access a spring singleton bean from such a class (in this case the hibernate usertype ) ?
I hope that my question was clear enough, thanks in advance for your support!
Mrik


Reply With Quote
