Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 363
pinkpanther Avatar asked Aug 02 '16 11:08

pinkpanther


2 Answers

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

like image 94
Steve Ebersole Avatar answered Oct 06 '22 01:10

Steve Ebersole


type is the equivalent xml attribute for Convert annotation.

Below is to convert to Y/N in DB and Boolean in entity.

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

Just replace yes_no with your custom converter class

Please see my answer at https://stackoverflow.com/a/37914271/3344829

Official documentation 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
    }

}
like image 35
Saravana Avatar answered Oct 05 '22 23:10

Saravana