How do I get Hibernate to call my custom typedef?

这一生的挚爱 提交于 2019-12-10 13:11:48

问题


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


回答1:


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

  • 2.4.3.2. Type

Resources

  • Hibernate CompositeUserType and Annotations


来源:https://stackoverflow.com/questions/3815965/how-do-i-get-hibernate-to-call-my-custom-typedef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!