How do I get Hibernate to call my custom typedef?

Steve picture Steve · Sep 28, 2010 · Viewed 13.9k times · Source

I'm trying to define a CompositeUserType to handle a specific type in my JPA/Hibernate app. I have a CompositeUserType called ApplicationMessageType that is designed to handle my mapping.

According to what I've read, I should be able to create a package-info.java class in my domain hierarchy that contains the TypeDefs. Mine looks like this:

@TypeDefs({
    @TypeDef(
        defaultForType = ApplicationMessage.class,
        typeClass = ApplicationMessageType.class
    )
})
package mptstp.domain;

import mptstp.domain.messages.ApplicationMessage;

import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

If I understand correctly, the fact that I'm using the defaultForType parameter to the TypeDef, anytime I attempt to save or load an ApplicationMessage, the custom type code should be called.

I've set breakpoints on every method within the ApplicationMessageType class, and none of them ever get called.

Does anyone have any idea where I've gone wrong? The code compiles, but it appears that the TypeDef annotations never got called to register the ApplicationMessageType.

Any pointers would be greatly appreciated...

Thanks

Answer

Pascal Thivent picture Pascal Thivent · Sep 29, 2010

I'm trying to define a CompositeUserType to handle a specific type in my JPA/Hibernate app. I have a CompositeUserType called ApplicationMessageType that is designed to handle my mapping.

Keep in mind that you'll have to declare the columns (using the @Columns annotation) at the field or property level for your composite user type.

According to what I've read, I should be able to create a package-info.java class in my domain hierarchy that contains the TypeDefs.

This is correct and this is the recommended strategy when the custom type is used in more than one entity.

If I understand correctly, the fact that I'm using the defaultForType parameter to the TypeDef, anytime I attempt to save or load an ApplicationMessage, the custom type code should be called.

In theory, the defaultForType element means that when Hibernate encounters a property of class ApplicationMessage, it is supposed to delegate the persistence strategy to the custom mapping type ApplicationMessageType.

But I'm sure how this works with a composite user type though, I'm not sure if you'll have to repeat the @Type to declare the @Columns (in which case, you should declare a name alias in the TypeDef).

Does anyone have any idea where I've gone wrong? The code compiles, but it appears that the TypeDef annotations never got called to register the ApplicationMessageType..

Can't say. Did you actually succeed to persist an entity with a property of class ApplicationMessage or not at all? Maybe show an annotated entity.

Reference

Resources