休眠hbm文件中的@Convert等效于什么? [英] What's the equivalent of @Convert in hibernate hbm file?

查看:89
本文介绍了休眠hbm文件中的@Convert等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个属性转换器。我想将其应用于实体。到目前为止,我一直在使用纯XML方法。



我在<中找不到与 @Convert 等效的内容。 em> hbm 表示法。



一个例子,将不胜感激。



当我搜索此内容时,Google会返回许多有关



编辑:
现在,我怀疑是否存在这样的工具/方法:自动将hbm文件转换为实体,反之亦然。

@Convert 的文档说:


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


我不是完全确定这意味着什么。在这种情况下,是否可以混合使用注释和XML?



我已经尝试过:

  public class Person {
//这是枚举
private种族种族;
// .....
}

公共枚举民族{
INDIAN( IND),
PERSIAN( PER)
// ...构造函数和值字段。

public String value(){
返回this.value;
}

公共种族fromValue(String value){
//转换逻辑
}
}

转换器:

  @转换器
公共类EthnicityConverter实现AttributeConverter< Ethnicity,String> {

@Override
public种族convertToEntityAttribute(String attribute){
if(attribute == null){
返回null;
}

返回种族。fromValue(属性);
}

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

返回dbData.value();
}
}

HBM文件:

  // ....其他列
<属性名称=种族>
<列名=种族 />
< type name = EthnicityConverter />
< / property>
// ....其他列

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

解决方案

Sarvana的答案很接近-您实际上使用的是类型 XML属性。但是, type 用于命名Hibernate Type 。但是,有一个命名约定,而不是AttributeConverter-只需将前缀 converted :: 应用于AttributeConverter FQN。例如

 < property name = ethnicity> 
<列名=种族 />
< type name = converted :: EthnicityConverter />
< / property>

另一种选择是自动应用转换器:

  @Converter(autoApply = true)
公共类EthnicityConverter实现AttributeConverter< Ethnicity,String> {
...
}

给出上面的转换器,只要Hibernate知道这一点,Hibernate会将其应用于任何类型为 Ethnicity 的属性。



HTH


I wrote a an attribute converter. I want to apply that in an entity. I'm following a purely XML approach so far.

I could not find an equivalent of @Convert in hbm notation.

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

Edit: Now I'm suspecting if there is an option in hbm file, given that this is JPA annotation.

The doc of @Convert says:

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.

I'm not entirely sure what it means. Is mixing annotation and XML a way to go in this case?

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:

//....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. However, type is used to name a Hibernate Type. However there is a convention to name, instead, an AttributeConverter - simply apply the prefix converted:: to your AttributeConverter FQN. E.g.,

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

HTH

这篇关于休眠hbm文件中的@Convert等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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