This is the code of my custom type:
Code:
package persistence.hibernate.testbench.customtypetest;
import net.sf.hibernate.CompositeUserType;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.type.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.io.Serializable;
public class MonetoryAmountType implements CompositeUserType, Serializable
{
public String[] getPropertyNames()
{
return new String[]{"value", "currency"};
}
public Type[] getPropertyTypes()
{
return new Type[]{Hibernate.INTEGER, Hibernate.INTEGER};
}
public Object getPropertyValue( Object component, int property ) throws HibernateException
{
Object result;
MonetoryAmount monetoryAmount = (MonetoryAmount)component;
switch (property)
{
case 0:
result = new Integer( monetoryAmount.getValue() );
break;
case 1:
result = new Integer( monetoryAmount.getCurrency() );
break;
default:
throw new HibernateException( "Unknown property: " + property );
}
return result;
}
public void setPropertyValue( Object component, int property, Object value ) throws HibernateException
{
MonetoryAmount monetoryAmount = (MonetoryAmount)component;
switch (property)
{
case 0:
monetoryAmount.setValue( ((Integer)value).intValue() );
break;
case 1:
monetoryAmount.setCurrency( ((Integer)value).intValue() );
break;
default:
throw new HibernateException( "Unknown property: " + property );
}
}
public Class returnedClass()
{
return MonetoryAmount.class;
}
public boolean equals( Object x, Object y ) throws HibernateException
{
if (x == y) return true;
if (x == null || y == null) return false;
MonetoryAmount monAmount1 = (MonetoryAmount)x;
MonetoryAmount monAmount2 = (MonetoryAmount)y;
if( monAmount1.getValue() != monAmount2.getValue() )
return false;
if( monAmount1.getCurrency() != monAmount2.getCurrency() )
return false;
return true;
}
public Object nullSafeGet( ResultSet rs, String[] names, SessionImplementor session, Object owner ) throws HibernateException, SQLException
{
Integer value = (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[0]);
Integer currency = (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[1]);
Object result;
if( value == null || currency == null )
result = null;
else
result = new MonetoryAmount(value.intValue(), currency.intValue());
return result;
}
public void nullSafeSet( PreparedStatement st, Object value, int index, SessionImplementor session ) throws HibernateException, SQLException
{
MonetoryAmount amount;
if( value == null )
amount = new MonetoryAmount();
else
amount = (MonetoryAmount)value;
Hibernate.INTEGER.nullSafeSet(st, new Integer(amount.getValue()), index);
Hibernate.INTEGER.nullSafeSet(st, new Integer(amount.getCurrency()), index+1);
}
public Object deepCopy( Object value ) throws HibernateException
{
Object result;
if( value == null )
{
result = null;
}
else
{
MonetoryAmount source = (MonetoryAmount)value;
result = new MonetoryAmount(source.getValue(), source.getCurrency());
}
return result;
}
public boolean isMutable()
{
return true;
}
public Serializable disassemble( Object value, SessionImplementor session ) throws HibernateException
{
return (Serializable)deepCopy(value);
}
public Object assemble( Serializable cached, SessionImplementor session, Object owner ) throws HibernateException
{
return deepCopy(cached);
}
}
This is my MonetoryAmount class:
Code:
package persistence.hibernate.testbench.customtypetest;
public class MonetoryAmount
{
public static final int EURO = 1;
private int _value;
private int _currency = EURO;
public MonetoryAmount()
{
}
public MonetoryAmount( int value )
{
_value = value;
}
public MonetoryAmount( int value, int currency )
{
_value = value;
_currency = currency;
}
public int getValue()
{
return _value;
}
public void setValue( int value )
{
_value = value;
}
public int getCurrency()
{
return _currency;
}
public void setCurrency( int currency )
{
_currency = currency;
}
public String toString()
{
return _value + " euro";
}
}