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
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With