Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hbm.dao.postgres.usertype;
  
  import java.sql.ResultSet;
  import java.sql.Types;
  
Note: Only use with Postgres and hibernate!!! The Class PGEnumUserType can be used only with Postgres database and hibernate.
 
 public class PGEnumUserType implements EnhancedUserTypeParameterizedType
 {

The enum class.
 
 	@SuppressWarnings("rawtypes")
 	private Class<EnumenumClass;

Parameters:
parameters
 
 	@SuppressWarnings({ "rawtypes""unchecked" })
 	public void setParameterValues(Properties parameters)
 	{
 		String enumClassName = parameters.getProperty("enumClassName");
 		try
 		{
 			 = (Class<Enum>)Class.forName(enumClassName);
 		}
 		catch (ClassNotFoundException cnfe)
 		{
 			throw new HibernateException("Enum class not found"cnfe);
 		}
 	}

 
 	public Object assemble(Serializable cachedObject ownerthrows HibernateException
 	{
 		return cached;
 	}

 
 	public Object deepCopy(Object valuethrows HibernateException
 	{
 		return value;
 	}

 
 	@SuppressWarnings("rawtypes")
 	{
 		return (Enum)value;
 	}

 
 	public boolean equals(Object oneObject anotherthrows HibernateException
 	{
 		return one == another;
 	}

 
 	public int hashCode(Object objectthrows HibernateException
 	{
 		return object.hashCode();
 	}

 
 	public boolean isMutable()
 	{
 		return false;
 	}

Null safe get.

Parameters:
rs the rs
names the names
owner the owner
Returns:
the object
Throws:
org.hibernate.HibernateException the hibernate exception
java.sql.SQLException the sQL exception
	@SuppressWarnings("unchecked")
	public Object nullSafeGet(ResultSet rsString[] namesObject owner)
	{
		Object object = rs.getObject(names[0]);
		if (rs.wasNull())
		{
			return null;
		}
		// Notice how Object is mapped to PGobject. This makes this implementation Postgres specific
		if (object instanceof PGobject)
		{
			PGobject pg = (PGobject)object;
			return Enum.valueOf(pg.getValue());
		}
		return null;
	}

Null safe set.

Parameters:
st the st
value the value
index the index
Throws:
org.hibernate.HibernateException the hibernate exception
java.sql.SQLException the sQL exception
	@SuppressWarnings("rawtypes")
	public void nullSafeSet(PreparedStatement stObject valueint index)
	{
		if (value == null)
		{
			st.setNull(index.);
			// UPDATE: To support NULL insertion, change to: st.setNull(index, 1111);
		}
		else
		{
			// Notice 1111 which java.sql.Type for Postgres Enum
			st.setObject(index, ((Enum)value), .);
		}
	}

	public Object replace(Object originalObject targetObject ownerthrows HibernateException
	{
		return original;
	}

	@SuppressWarnings("rawtypes")
	{
		return ;
	}

	public int[] sqlTypes()
	{
		return new int[] { . };
		// UPDATE: To support NULL insertion, change to: return new int[] { 1111 };
	}

	@SuppressWarnings("unchecked")
	public Object fromXMLString(String xmlValue)
	{
		return Enum.valueOf(xmlValue);
	}

	@SuppressWarnings("rawtypes")
	{
		return '\'' + ((Enum)value).name() + '\'';
	}

	@SuppressWarnings("rawtypes")
	public String toXMLString(Object value)
	{
		return ((Enum)value).name();
	}

	public Object nullSafeGet(ResultSet rsString[] namesSessionImplementor sessionObject owner)
	{
		// TODO research of implementation with SessionImplementor class...
		return nullSafeGet(rsnamesowner);
	}

	public void nullSafeSet(PreparedStatement stObject valueint index,
	{
		// TODO research of implementation with SessionImplementor class...
		nullSafeSet(stvalueindex);
	}
New to GrepCode? Check out our FAQ X