简体   繁体   English

在hibernate hbm文件中@Convert的等价物是什么?

[英]What's the equivalent of @Convert in hibernate hbm file?

I wrote a an attribute converter. 我写了一个属性转换器。 I want to apply that in an entity. 我想在一个实体中应用它。 I'm following a purely XML approach so far. 到目前为止,我正在遵循纯粹的XML方法。

I could not find an equivalent of @Convert in hbm notation. hbm表示法中找不到@Convert的等价物。

An example would be appreciated. 一个例子将不胜感激。

When I search for this, understandably, Google returns lots of results about tools/methods on "Auto Converting hbm files to entities vice versa". 当我搜索这个时,可以理解的是,Google会在“自动将hbm文件转换为实体,反之亦然”上返回有关工具/方法的大量结果。

Edit: Now I'm suspecting if there is an option in hbm file, given that this is JPA annotation. 编辑:现在我怀疑hbm文件中是否有选项,因为这是JPA注释。

The doc of @Convert says: @Convert的文档说:

The Convert annotation is used to specify the conversion of a Basic field or property. 转换注释用于指定基本字段或属性的转换。 It is not necessary to use the Basic annotation or corresponding XML element to specify the basic type. 没有必要使用Basic注释或相应的XML元素来指定基本类型。

I'm not entirely sure what it means. 我不完全确定它意味着什么。 Is mixing annotation and XML a way to go in this case? 在这种情况下,混合注释和XML是一种方法吗?

I've tried this: 我试过这个:

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: 转换器:

@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 File: HBM文件:

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

Edit : Corrected the converter code. 编辑 :更正了转换器代码。

The answer from Sarvana is close - you do in fact use the type XML attribute. Sarvana的答案很接近 - 事实上您确实使用XML type属性。 However, type is used to name a Hibernate Type . 但是, type用于命名Hibernate Type However there is a convention to name, instead, an AttributeConverter - simply apply the prefix converted:: to your AttributeConverter FQN. 但是,有一个约定命名为AttributeConverter - 只需将prefix converted::前缀应用于AttributeConverter FQN。 Eg, 例如,

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

The other option is to auto-apply the converter: 另一种选择是自动应用转换器:

@Converter( autoApply=true)
public class EthnicityConverter implements AttributeConverter<Ethnicity,String> {
    ...
}

Given the converter above, so long as Hibernate knows about it, Hibernate will apply that to any attribute of type Ethnicity . 鉴于上面的转换器,只要Hibernate知道它,Hibernate就会将其应用于Ethnicity类型的任何属性。

HTH HTH

type is the equivalent xml attribute for Convert annotation. typeConvert annotation的等效xml属性。

Below is to convert to Y/N in DB and Boolean in entity. 下面是转换为DB中的Y / N和实体中的布尔值。

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

Just replace yes_no with your custom converter class 只需将yes_no替换为您的自定义converter

Please see my answer at https://stackoverflow.com/a/37914271/3344829 请通过https://stackoverflow.com/a/37914271/3344829查看我的答案

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

Update 更新

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

Update 更新

@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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM