如何让 Hibernate 调用我的自定义 typedef?

发布于 2024-09-25 03:41:54 字数 784 浏览 8 评论 0原文

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

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

@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 时,都应该调用自定义类型代码。

我已经在 ApplicationMessageType 类中的每个方法上设置了断点,但它们都没有被调用。

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

任何指点将不胜感激...

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-10-02 03:41:54

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

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

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

这是正确的,并且当在多个实体中使用自定义类型时,这是推荐的策略。

如果我理解正确,事实上我正在使用 TypeDef 的 defaultForType 参数,每当我尝试保存或加载 ApplicationMessage 时,都应该调用自定义类型代码。

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

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

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

不能说。您是否确实成功地保留了具有类 ApplicationMessage 属性的实体,还是根本没有成功?也许显示一个带注释的实体。

参考

资源

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文