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

标签 hibernate annotations typedef hibernate-annotations

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

根据我所读到的,我应该能够在包含 TypeDefs 的域层次结构中创建一个 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.



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

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.



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

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 element 表示当 Hibernate 遇到类 ApplicationMessage 的属性时,应该将持久化策略委托(delegate)给自定义映射类型ApplicationMessageType .

但我确定这如何与复合用户类型一起使用,我不确定您是否必须重复 @Type声明@Columns (在这种情况下,您应该在 name 中声明 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..



不能说。你真的成功地持久化了一个具有类 ApplicationMessage 属性的实体吗?还是根本没有?也许显示一个带注释的实体。

引用
  • 2.4.3.2. Type

  • 资源
  • Hibernate CompositeUserType and Annotations
  • 关于hibernate - 如何让 Hibernate 调用我的自定义 typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3815965/

    相关文章:

    java - 如何将连接查询映射到 JPA 中的非实体类?

    java - Autowiring 集合

    java - 我可以将模板类中的类文字作为注释的参数传递吗?

    c++ - typedef 语法错误

    c - 使用 typedef 结构时出现错误 'a value of type "X *"cannot be assigned to an entity of type "X *"'

    java - Hibernate 无法解析带有枚举的类型

    java - Hibernate 枚举设置错误

    Hibernate查询带有ID的外键字段

    android - @interface - 什么?

    C++ 类型定义错误