-->
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.  [ 2 posts ] 
Author Message
 Post subject: Dao access in a UserType
PostPosted: Tue May 09, 2006 8:05 am 
Newbie

Joined: Thu Apr 06, 2006 2:09 pm
Posts: 14
According to the following article, I18N data with hibernate, I'm implementing a usertype that access to an Hibernate style DAO to retrieve the code or the text of the loclalized label.

My usertype nullSafeGet and nullSafeSet methods are like this :

Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException {

       String code = (String) Hibernate.STRING.nullSafeGet(rs, names);
       return LocalizedLabelManager.getText(code, LocaleUtil.currentLocale());

}


Code:
public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
           
      String code = LocalizedLabelManager.getCode( (String) value, LocaleUtil.currentLocale());
      Hibernate.STRING.nullSafeSet(st, code, index);

}


My LocalizedLabelManager access a LocalizedLabelDao for the method getCode() :

Code:
public static  String getCode(String text, Locale userLocale){

     String code = localizedLabelDao.getCode(text, userLocale);
     if(code == null){
         code = generateCode(text, userLocale);
         LocalizedLabel localizedLabel = new LocalizedLabel(userLocale, text);
         localizedLabelDao.saveOrUpdate(localizedLabel);
     }
       
     return code;
}


And here is the getCode method of my LocalizedLabelDao:

Code:
public String getCode(String text, Locale locale) {

     return (String) this.getSession().createQuery("from LocalizedLabel ll where ll.text = :text and ll.locale = :locale")
        .setString("text", text)
        .setLocale("locale", locale).setCacheable(true).uniqueResult();

}


A localizedLabel is a traditional POJO.

The problem is when I try to save an object that has a LocalizedLabel, like my Category object below :

Code:
@Entity
public class Category {
   
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @Type(type="LocalizedLabelUserType")
    private String name;
    @Type(type="LocalizedLabelUserType")
    private String description;
    @ManyToOne
    private Category parent;

    //Other methods are not relevant

}



I get the folowing exception :
Exception in thread "main" org.hibernate.AssertionFailure: null id in my.package.Category entry (don't flush the Session after an exception occurs)

Which is trown by the DefaultFlushEntityEventListener that check id's before it flushes the session before executing the query in getCode() method.
But it's normal that an id is null as, at the time of this query excution, the save operation on my category is pending.

Even if I understand that the session needs to be flushed before a query execution, is there a way to handle that issue? Is it bad design to access a DAO in a usertype (indeed, it sound weird) ?

Thanks in advance,

-Aymeric


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 10:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Is the session you access in LocalizedLabelDao the same session that is being flushed to call into your UserType in the first place?


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

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.