Is there a way (locally) to bypass or disable the AttributeConverter while calling the criteria API?

Suppose a CreditcardNumb.classwith a constructor that checks if the credit card number is valid.

Then I create CreditcardNumbConverter.classto convert credit card numbers to rows in the database:

public class CreditcardnumbConverter
      implements AttributeConverter<CreditcardNumb, String> {

    public String convertToDatabaseColumn(CreditcardNumb cn) {
        if (cn== null) return null;
        return cn.toString();
        }

    public CreditcardNumb convertToEntityAttribute(String cn) {
        if ((cn == null) || cn.trim().isEmpty()) return null;
        return new CreditcardNumb(cn);
        }
    }

This works fine, but now I want to use the Hibernate Criteria API (new or old API) to search for credit card numbers starting with "123":

CreditcardNumb cn = new CreditcardNumb("123");

createCriteria(Wallet.class)
      .add(Restrictions.ilike("creditcard", cn, MatchMode.START))
      .list();

However, since "123" is not a valid credit card number, the instance of the object fails. In addition, this does not even compile, since the method ilikeaccepts only Strings, not CreditcardNumbs.

My question is:

() , - :

createCriteria(Wallet.class)
      .disable(CreditcardnumbConverter.class))
      .add(Restrictions.ilike("creditcard", "123", MatchMode.START))
      .list();

, "123" , API ( HQL).

+4
1

String:

@Entity
public class Wallet {

  private CreditcardNumb creditcard;

  @Column(name = ..., insertable = false, updatable = false)
  private String creditcardStr;

  ...
}

, , ( Hibernate , creditcard creditcardStr ).

:

createCriteria(Wallet.class)
  .add(Restrictions.ilike("creditcardStr", "123", MatchMode.START))
  .list();
+2

Source: https://habr.com/ru/post/1667599/


All Articles