在Hibernate HBM文件中,与@Convert等效的是什么?

5

我写了一个属性转换器。我想在实体中应用它。到目前为止,我一直遵循纯XML的方法。

我在符号中找不到@Convert的等效物。

举个例子会很好。

当我搜索此内容时,可以理解地,谷歌返回了许多关于“自动转换hbm文件和实体”的工具/方法的结果。

编辑: 现在我怀疑是否有hbm文件中的选项,因为这是JPA注释。

@Convert的文档说:

Convert注释用于指定基本字段或属性的转换。不必使用Basic注释或相应的XML元素来指定基本类型。

我不完全确定它的意思。在这种情况下,混合注释和XML是一种选择吗?

我尝试过这个:

public class Person {
   //this is enum
   private Ethnicity ethnicity;
   //.....
}

public enum Ethnicity{
   INDIAN("IND"),
   PERSIAN("PER")
   //...constructors and value field.

   public String value(){
     return this.value;
   }

   public Ethnicity fromValue(String value){
       //logic for conversion
   }
}

转换器:

@Converter
public class EthnicityConverter implements AttributeConverter<Ethnicity,String> {

        @Override
        public Ethnicity convertToEntityAttribute(String attribute) {
            if ( attribute == null ) {
                return null;
            }

            return Ethnicity.fromValue( attribute );
        }

        @Override
        public String convertToDatabaseColumn(Ethnicity dbData) {
            if ( dbData == null ) {
                return null;
            }

            return dbData.value();
        }
}

HBM文件:

//....other columns
 <property name="ethnicity">
            <column name="ethnicity"/>
            <type name="EthnicityConverter"/>
        </property>
//....other columns

编辑:修正了转换器代码。


JPA不使用Hibernate配置文件,而是使用orm.xml。如果您没有使用JPA,请删除JPA标签。 - Neil Stockton
@NeilStockton 我正在使用Hibernate,我相信Hibernate也是JPA的一种实现,而Convert来自javax.persistence。我是对的吗?如果还有什么不对的地方,我会进行更改。谢谢。我们在hbm文件中有EnumType.ORDINAL、STRING的等效项,因此才会问这个问题。 - pinkpanther
是的,Hibernate确实实现了JPA,但JPA不使用"hbm"文件。JPA使用orm.xml。如果您使用的是hbm文件,则仅使用Hibernate。 - Neil Stockton
1
如果有用的话,“convert”的orm.xml定义在此链接中:http://www.datanucleus.org/products/accessplatform_5_0/jpa/metadata_xml.html#convert - Neil Stockton
@pinkpanther,您希望将枚举的字符串值还是实际类型持久化到数据库中? - Saravana
显示剩余3条评论
3个回答

6
萨瓦纳的回答接近正确 - 事实上,您确实使用type XML属性。但是,type用于命名Hibernate Type。然而,有一种约定来命名AttributeConverter - 只需将前缀converted ::应用于AttributeConverter FQN即可。例如,
 <property name="ethnicity">
            <column name="ethnicity"/>
            <type name="converted::EthnicityConverter"/>
 </property>

另一种选择是自动应用转换器:
@Converter( autoApply=true)
public class EthnicityConverter implements AttributeConverter<Ethnicity,String> {
    ...
}

假如Hibernate知道上面的转换器,那么Hibernate会将其应用于任何类型为Ethnicity的属性。

希望对你有所帮助。


我已经在转换器中设置了autoapply=true,但是Hibernate没有使用它。使用此解决方案中提到的第一个选项起作用 - > 前缀converted :: 在2018年11月进行了测试。当从jpa-xml更改为hibernate-xml时,奇怪的是autoapply=true对我没有起作用。 - Jhovanni
这告诉我你没有告诉Hibernate关于那个转换器类。 - Steve Ebersole

2

typeConvert注解的等效xml属性。

以下内容是将数据库中的值转换为Y/N,并在实体中使用Boolean类型。

<property name="status" column="book_status" type="yes_no" not-null="true"/>

只需用您自定义的converter类替换yes_no

请查看我的回答 https://stackoverflow.com/a/37914271/3344829

官方文档 https://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch06.html

更新

<property name="ethnicity" column="ethnicity" type="com.example.EthnicityConverter"/>

更新

@Converter
public class EthnicityConverter implements AttributeConverter<Ethnicity, String> {

    @Override
    public String convertToDatabaseColumn(Ethnicity attribute) {
        // TODO return String value of enum
    }

    @Override
    public Ethnicity convertToEntityAttribute(String dbData) {
        // TODO return resolved enum from string
    }

}

1
但实体仍将具有枚举的实际类型? - pinkpanther
是的,使用 Convert 可以在持久化和检索时更改类型,因此实体和数据库中的数据类型将不同。 - Saravana
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - pinkpanther
你能在问题中发布hbm映射文件、转换器代码和异常吗? - Saravana
@pinkpanther 我更新了答案,在类型属性中必须提及完整限定名称而不仅是类名。 - Saravana
显示剩余9条评论

1

我也曾面对这个问题,但最终解决了。

参考:Hibernate 5 文档 示例 33. AttributeConverter 的 HBM 映射

我们必须使用带有包名的确切类。

例如:

<property name="ethnicity" column="ethnicity" type="converted::com.example.EthnicityConverter"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接