Hibernate.custom(MyType.class)



|---------+------------------------------------------->
|         |           Jenica Humphreys                |
|         |           <[EMAIL PROTECTED]>               |
|         |           Sent by:                        |
|         |           [EMAIL PROTECTED]|
|         |           ceforge.net                     |
|         |                                           |
|         |                                           |
|         |           31/07/03 10:47 AM               |
|         |                                           |
|---------+------------------------------------------->
  
>------------------------------------------------------------------------------------------------------------------------------|
  |                                                                                    
                                          |
  |       To:       [EMAIL PROTECTED]                                                  
                      |
  |       cc:                                                                          
                                          |
  |       Subject:  [Hibernate] HQL, User defined type                                 
                                          |
  
>------------------------------------------------------------------------------------------------------------------------------|




I'm /still/ using Hibernate 1.25 -

I have a user-defined "ThreeStateType" type that implements the UserType
interface.  The getter and setter each use a ThreeState class that's
implemented using the enumeration pattern.  (The class has only 3
instantiations and can't be externally instantiated.  Possible values are
Yes/No/Unknown.)

The ThreeStateType is used in the hbm.xml file, and the ThreeState class is

used in the business object.

I can load and save these objects beautifully with hibernate.  Where I'm
having trouble is running a query - For a long parameter in HQL I pass in
the Long and Hibernate.LONG as the type.  What do I pass in for this user
defined type?

Thanks for your help!

Jenica Humphreys

-------------------------
ThreeState.java:
package com.mvsc.czee.persistence;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class ThreeState extends ThreeStateType {
   public static final ThreeState Unknown = new ThreeState("U");
   public static final ThreeState Yes = new ThreeState("Y");
   public static final ThreeState No = new ThreeState("N");

   private final String myName; // for debug only

   private ThreeState(String name) {
     myName = name;
   }

   public String toString() {
     return myName;
   }

   public boolean equals(Object other) {
     boolean result = false;
     if (other instanceof ThreeState) {
       ThreeState castOther = (ThreeState) other;
       result = new EqualsBuilder()
           .append(this.myName, castOther.myName)
           .isEquals();
     }
     return result;
   }

   public int hashCode() {
     return new HashCodeBuilder()
         .append(myName)
         .toHashCode();
   }

}

----------------------------
ThreeStateType.java:
package com.mvsc.czee.persistence;

import cirrus.hibernate.UserType;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.Hibernate;

import java.sql.Types;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class ThreeStateType implements UserType {
   private static final int[] TYPES = {Types.STRING;

   public int[] sqlTypes() {
     return TYPES;
   }

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

   public boolean equals(Object o, Object o1) {
     boolean result = false;
     if ((o != null) && (o1 != null)) {
       result = o.equals(o1);
     }
     return result;
   }

   public Object nullSafeGet(ResultSet resultSet, String[] names, Object o)

throws HibernateException, SQLException {
     Object result = ThreeState.Unknown;
     String first = (String) Hibernate.STRING.nullSafeGet(resultSet,
names[0]);
     if (first == null) {
       result = ThreeState.Unknown;
     } else if (first.equals("Y")) {
       result = ThreeState.Yes;
     } else if (first.equals("N")) {
       result = ThreeState.No;
     }
     return result;
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
     String fieldValue = null;
     if (value == null) {
       fieldValue = ThreeState.Unknown.toString();
     } else if (value instanceof ThreeState) {
       fieldValue = value.toString();
     } else {
       fieldValue = ThreeState.Unknown.toString();
     }

     Hibernate.STRING.nullSafeSet(st, fieldValue, index);
   }

   public Object deepCopy(Object o) {
     return o;  //it's a enum... I can't really copy it
   }

   public boolean isMutable() {
     return false;
   }
}



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel




**********************************************************************
Any personal or sensitive information contained in this email and
attachments must be handled in accordance with the Victorian Information
Privacy Act 2000, the Health Records Act 2001 or the Privacy Act 1988
(Commonwealth), as applicable.

This email, including all attachments, is confidential.  If you are not the
intended recipient, you must not disclose, distribute, copy or use the
information contained in this email or attachments.  Any confidentiality or
privilege is not waived or lost because this email has been sent to you in
error.  If you have received it in error, please let us know by reply
email, delete it from your system and destroy any copies.
**********************************************************************






-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to