如何让 Hibernate 调用我的自定义 typedef? [英] How do I get Hibernate to call my custom typedef?

查看:38
本文介绍了如何让 Hibernate 调用我的自定义 typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个 CompositeUserType 来处理我的 JPA/Hibernate 应用程序中的特定类型.我有一个名为 ApplicationMessageType 的 CompositeUserType,用于处理我的映射.

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.

根据我所读到的内容,我应该能够在我的域层次结构中创建一个包含 TypeDef 的 package-info.java 类.我的看起来像这样:

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;

如果我理解正确的话,我在 TypeDef 中使用 defaultForType 参数这一事实,无论何时我尝试保存或加载 ApplicationMessage,都应该调用自定义类型代码.

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.

我在 ApplicationMessageType 类中的每个方法上都设置了断点,并且没有一个永远被调用.

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

有谁知道我哪里出错了?代码可以编译,但似乎从未调用 TypeDef 注释来注册 ApplicationMessageType.

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...

谢谢

推荐答案

我正在尝试定义一个 CompositeUserType 来处理我的 JPA/Hibernate 应用程序中的特定类型.我有一个名为 ApplicationMessageType 的 CompositeUserType,用于处理我的映射.

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.

请记住,您必须声明列(使用 @Columns 注释)在您的复合用户类型的字段或属性级别.

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.

根据我所读到的内容,我应该能够在包含 TypeDefs 的域层次结构中创建一个 package-info.java 类.

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.

如果我理解正确的话,我在 TypeDef 中使用 defaultForType 参数这一事实,无论何时我尝试保存或加载 ApplicationMessage,都应该调用自定义类型代码.

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.

理论上,defaultForType元素意味着当Hibernate遇到ApplicationMessage类的属性时,应该将持久化策略委托给自定义映射类型应用消息类型.

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.

但我确定这是如何处理复合用户类型的,我不确定您是否必须重复 @Type 来声明 @Columns(在这种情况下,您应该在 TypeDef 中声明一个 name 别名).

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).

有谁知道我哪里出错了?代码可以编译,但似乎从未调用 TypeDef 注释来注册 ApplicationMessageType..

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..

不能说.您是否真的成功地持久化了具有 ApplicationMessage 类属性的实体,或者根本没有成功?也许显示一个带注释的实体.

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.

这篇关于如何让 Hibernate 调用我的自定义 typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆