-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Turn off 2nd level caching for UserType?
PostPosted: Tue Feb 04, 2014 6:12 pm 
Newbie

Joined: Tue Feb 04, 2014 6:00 pm
Posts: 1
Hi, I struggle with the i18n setup in my application. I have now tried to follow this recipe: http://www.theserverside.com/news/1377072/Internationalized-Data-in-Hibernate

And it works as long as I don't use the second level cache. When I do use it the first value is cached and return even if I later change the language. (Through the disassemble and assemble methods). I really hope someone can help me with this one, cause I need to cache the CarType entity, just not the userType-part. (The I18nLocaleHolder operates on an in-memory map)

Here is part of the entity class:
Code:
@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;

        ...


And here is the userType:
Code:
public class LocalizedLabelUserType implements 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;
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.