Home > Mobile >  How to implement a Custom DataType in Hibernate?
How to implement a Custom DataType in Hibernate?

Time:12-14

We would like to type various properties in Java. e.g. the e-mail address

But now I get the message all the time:

Could not set field value [[email protected]] value by reflection : [class customer.email] setter of customer.email;

Can not set dataType.EmailAddress field customer.email to java.lang.String

How should I proceed?

@Entity
public class customer {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private EmailAddress email;
}


public class EmailAddress {

    public String value;

    public EmailAddress(String value) {
        this.value = value;
    }

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

    public boolean setValue(String s) {
        this.value = s;
        return true;
    }

   public String mailbox() ...
   public String host() ...
   public String tld() ...
}

Getter and Setter from HibernateDefaultType not called.

CodePudding user response:

it is much easier. An AttributeConverter make it very easy.

https://thorben-janssen.com/jpa-attribute-converter/

Thank you very much

CodePudding user response:

Here is some example of making your own custom type.

public class EmailAddressDescriptor extends AbstractTypeDescriptor<String> {

    protected EmailAddressDescriptor() {
        super(String.class, new ImmutableMutabilityPlan<>());
    }

    @Override
    public String toString(String value) {
        return null;
    }

    @Override
    public String fromString(String string) {
        return null;
    }

    @Override
    public <X> X unwrap(String value, Class<X> type, WrapperOptions options) {
        return null;
    }

    @Override
    public <X> String wrap(X value, WrapperOptions options) {
        return null;
    }

    @Override
    public SqlTypeDescriptor getJdbcRecommendedSqlType(JdbcRecommendedSqlTypeMappingContext context) {
        return null;
    }


}

Then you would make the Email address class with all your methods

public class EmailAddress extends AbstractSingleColumnStandardBasicType<String> {
    private String value;

    public EmailAddress() {
        super(new VarcharTypeDescriptor(), new EmailAddressDescriptor());
    }

    @Override
    public String getName() {
        return "EmailAddress";
    }

    @Override
    public Object resolve(Object value, SharedSessionContractImplementor session, Object owner, Boolean overridingEager) throws HibernateException {
        return null;
    }

}
 public String mailbox() ...
   public String host() ...
   public String tld() ...

How you would use it with your entity will be something like this

@Entity
@TypeDef(name = "emailAddress", typeClass = EmailAddress.class)
public class customer {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @Type (type = "emailAddress")
    private EmailAddress emailAddress;
}

Hope this helps

  • Related