Results 1 to 3 of 3

Thread: i18n with Hibernate UserType and Spring

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    1

    Question i18n with Hibernate UserType and Spring

    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 )

    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
           ...
    }
    LocaleUtil.currentLocale() just returns a java.util.Locale object.
    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

  2. #2
    Join Date
    May 2006
    Posts
    1

    Default DAO Access in a static way

    I'm actually using the same pattern for I18N data using Hibernate and Spring.

    The only way I found to retreve data in a static way from the UserType is to define a Spring bean I18NLabel with the appropriate DAO injected. I make it implements InitializingBean so in the afterPropertySet method, I construct a static Map which contains a cache of all I18N descriptions.
    To acces this cache, I added a static method getDescription(...).

    This solution works fine to access I18N labels

    Now I still have two problems :
    1) looking for a solution to use the second level cache (ehcache for example) instead of a Map.
    2) allow I18N description writable.
    Last edited by masantiago; May 12th, 2006 at 04:28 AM.

  3. #3

    Default

    Quote Originally Posted by mrik View Post
    LocaleUtil.currentLocale() just returns a java.util.Locale object.
    I'll talk about LocalizedLabelUtil shortly.
    Can you do this explanation, please? I'm very interested in that.
    Spring 3.X, Web Flow 2.X, Java 6, STS 2.7.0, Maven, OS X

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •