-->
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.  [ 5 posts ] 
Author Message
 Post subject: boolean x varchar2
PostPosted: Fri Jul 22, 2005 3:49 pm 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Hi,

How do I map a varchar2 column ('0'=false, '1'=true) to a boolean property?

Thanks

Andre


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 9:04 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
you have use hibernate true_false type

regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 11:20 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
snpesnpe wrote:
you have use hibernate true_false type

regards


According to Hibernate in Action the values for true_false are are 'T' and 'F' (yes_no can be used for 'Y' and 'N'). I'm not sure whether there is a way to do this for '0' and '1' with these types (couldn't find it in a quick search of the reference manual, although it might be somewhere else). I would recommed searching the FAQs and other documentation to see if you can find more information about these types. Hopefully they'll work for you, but if they don't, there's always the oportunity to write a custom UserType (probably by subclassing org.hibernate.type.CharBooleanType).


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 23, 2005 10:20 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I use next for mapping varchar to boolean (can be null).It work for hibernate 3.x
Code:
package yu.co.snpe.dbtable.model.hibernate.type;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

/**
* @author snpe
*
*/
public class StringToBooleanUserType implements UserType,ParameterizedType {

    // default true value is 'D', default false = null
    private String stringTrue = "D";
    private String stringFalse = null;
   
    public void setParameterValues(Properties parameters) {
        stringTrue = parameters.getProperty("stringTrue");
        stringFalse = parameters.getProperty("stringFalse");
     }
   
   public int[] sqlTypes() {
      return new int[] { Types.VARCHAR };
   }

   public Class returnedClass() {
      return boolean.class;
   }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
         throws HibernateException, SQLException {
      String val = rs.getString(names[0]);
      if (null != val && val.equals(stringTrue))
         return true;
      return false;
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index)
         throws HibernateException, SQLException {
      if (((Boolean) value).booleanValue())
         st.setString(index, stringTrue);
      else if (stringFalse == null)
         st.setNull(index, Types.VARCHAR);
        else
            st.setString(index,stringFalse);
   }

   public Object deepCopy(Object value) throws HibernateException {
      return value;
   }

   public boolean isMutable() {
      return false;
   }

   public int hashCode(Object x) throws HibernateException {
      return x.hashCode();
   }

    public Object assemble(Serializable cached, Object owner) throws HibernateException  {
        return cached;
   }

   public Serializable disassemble(Object value) throws HibernateException {
       return (Serializable)value;
   }

   public Object replace(Object original, Object target, Object owner) throws HibernateException {
       return original;
   }
   
   public boolean equals(Object x, Object y) throws HibernateException {
       if (x == y)
           return true;
       if (null == x || null == y)
           return false;
       return x.equals(y);
   }
}


and for 0, 1 mapping is :
Code:
<typedef name="ZeroOrOne"
      class="yu.co.snpe.dbtable.model.hibernate.type.StringToBooleanUserType">
      <param name="stringTrue">1</param>
      <param name="stringFalse">0</param>
   </typedef>


regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 8:52 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Thank you very much snpesnpe!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.