Custom types using org.hibernate.usertype.CompositeUserType by R4R Team

When we go on the last approach that is the Custom types using org.hibernate.usertype.CompositeUserType. We can use of this approach by the interface that differ fro org.hibernate.usertype.UserType we found the ability to provide Hibernate. It gives the information to handle to the composition with in the HibernateMapping class (it has specially two attributes). It gives us the capability. we can understand by the example, it could by the reference the amount in an HQL query. if done when we use the org.hibernate.usertype.CompositeType.

How is this work it give in the below example to Defining the custom CompositeUserType:

public class HibernateMapping implements CompositeUserType 
{
      public String[] getPropertyNames() 
{
        // ORDER IS IMPORTANT!  it must match the order the columns are defined in the property mapping
        return new String[] { "amount", "currency" };
    }
    public Type[] getPropertyTypes() {
        return new Type[] { BigDecimalType.INSTANCE, CurrencyType.INSTANCE };
    }
    public Class getReturnedClass() {
        return HibernateMapping.class;
    }
    public Object getPropertyValue(Object component, int propertyIndex) {
        if ( component == null ) {
            return null;
        }
        final HibernateMapping hibernateType = (HibernateMapping) component;
        switch ( propertyIndex ) {
            case 0: {
                return hiberanteType.getAmount();
            }
            case 1: {
                return hiberanteType.getCurrency();
            }
            default: {
                throw new HibernateException( "Invalid property index [" + propertyIndex + "]" );
            }
        }
    }
  public void setPropertyValue(Object component, int propertyIndex, Object value) throws HibernateException {
        if ( component == null ) {
            return;
        }

        final HibernateType hibertype = (HibernateType) component;
        switch ( propertyIndex ) {
            case 0: {
                hibernateType.setAmount( (BigDecimal) value );
                break;
            }
            case 1: {
                hibernateType.setCurrency( (Currency) value );
                break;
            }
            default: {
                throw new HibernateException( "Invalid property index [" + propertyIndex + "]" );
            }
        }
    }
    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
        assert names.length == 2;
        BigDecimal amount = BigDecimalType.INSTANCE.get( names[0] ); // already handles null check
        Currency currency = CurrencyType.INSTANCE.get( names[1] ); // already handles null check
        return amount == null && currency == null
                ? null
                : new hibernateType( amount, currency );
    }
    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException {
        if ( value == null ) {
            BigDecimalType.INSTANCE.set( st, null, index );
            CurrencyType.INSTANCE.set( st, null, index+1 );
        }
        else {
            final HibernateMapping hibernateType = (HibernateMapping) value;
            BigDecimalType.INSTANCE.set( st, hibernateType.getAmount(), index );
            CurrencyType.INSTANCE.set( st, hibernateType.getCurrency(), index+1 );
        }
    }

    ...
}
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!