Should there be different wrap and unwrap class types when creating custom hibernate type class?

442 views Asked by At

When I create a custom hibernate Type, such as


public class CustomHibernateType extends AbstractSingleColumnStandardBasicType<Custom>
{
    public CustomHibernateType()
    {
        // Notice the JsonBinarySqlTypeDescriptor class from @vladmihalcea's hibernate types library
        super(JsonBinarySqlTypeDescriptor.INSTANCE, CustomHibernateTypeDescriptor.INSTANCE);
    }

    @Override
    public String getName()
    {
        return "CustomHibernateType";
    }
}

And create the Type Descriptor

    // only constructor for brevity, can include more if needed
    public CustomHibernateTypeDescriptor()
    {
        super(Custom.class, ImmutableMutabilityPlan.INSTANCE);
    }

I find that when I am saving and retrieving data in my custom hibernate type descriptor, the class type used is different. It is saved as a jackson JsonNode, which is expected as this is what is used in the JsonBinarySqlTypeDescriptor class, however when it is retrieved it is retrieved in the unwrap method as a PGobject (which has a simple string value).

Should I be expecting this, or should I be expecting JsonNode in both directions? I have no problems processing the PGobject, it just wasn't expected so I'm wondering if there is some config missing somewhere.

1

There are 1 answers

0
Vlad Mihalcea On

The wrap method transforms the entity attribute type to the one that should be passed to the JDBC PreparedStatement.

The unwrap method transforms the object that was extracted from the JDBC ResultSet to the expected entity attribute type.